有人告诉我,使用 bash 的人必须区分 shell-builtintime和/usr/bin/time( time(1))。我必须为程序计时,还必须找到自动输入、使用echo和字符串重定向的方法<<<。这些是结果,
$ time python3 -c "a=input("");print(a)" <<< "12"
12
real 0m0.023s
user 0m0.020s
sys 0m0.000s
Run Code Online (Sandbox Code Playgroud)
在这种情况下调用 shell-built-in。
$ echo "12" | time python3 -c "a=input("");print(a)"
12
0.01user 0.00system 0:00.02elapsed 100%CPU (0avgtext+0avgdata 6524maxresident)k
0inputs+0outputs (0major+593minor)pagefaults 0swaps
Run Code Online (Sandbox Code Playgroud)
time(1) 在这种情况下被调用。
为什么会这样?它们是不同的环境吗?
使用 Ubuntu 14.04 x86_64
小智 5
此外,时间是一个“shell 关键字”,如type time.
time comm1 | ( time comm2 )
Run Code Online (Sandbox Code Playgroud)
因此,您可以使用子外壳解决方法,如下所示:
echo "12" | ( time python3 -c "a=input("");print(a)" )
Run Code Online (Sandbox Code Playgroud)
或者也像这样:
echo "12" | { time python3 -c "a=input("");print(a)"; }
Run Code Online (Sandbox Code Playgroud)