如何使用 bash 内置 'time' 在 Python 变量中捕获挂钟时间和 CPU 时间?
How to capture wall-clock time and CPU time in a Python variable using bash builtin 'time'?
我正在编写 Python 脚本,该脚本将在命令行中 运行。这个想法是从用户那里得到一个命令,运行 然后提供挂钟时间和用户提供的命令的 CPU 时间。请参阅下面的代码。
#!/usr/bin/env python
import os
import sys
def execut_cmd(cmd_line):
utime = os.system('time '+cmd_line)
# Here I would like to store the wall-clock time in the Python variable
# utime.
cputime = os.system('time '+cmd_line)
# Here the CPU time in the cputime variable. utime and cputime are going to
# be used later in my Python script. In addition, I would like to silence the
# output of time in the screen.
execut_cmd(sys.argv[1])
print ('Your command wall-clock time is '+utime)
print ('Your command cpu time is '+ cputime)
我怎样才能做到这一点?另外,如果有比使用 'time' 更好的方法,我愿意尝试。
来自Python Documentation for wall time:
... On Windows, time.clock() has microsecond granularity, but time.time()’s granularity is 1/60th of a second. On Unix, time.clock() has 1/100th of a second granularity, and time.time() is much more precise. On either platform, default_timer() measures wall clock time, not the CPU time. This means that other processes running on the same computer may interfere with the timing.
对于挂钟时间,您可以使用 timeit.default_timer() 获取具有上述最佳粒度的计时器。
从 Python 3.3 及更高版本开始,您可以使用 time.process_time() 或 time.process_time_ns() 。以下是 process_time 方法的 the documentation entry:
Return the value (in fractional seconds) of the sum of the system and user CPU time of the current process. It does not include time elapsed during sleep. It is process-wide by definition. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.
我花了很少的时间在 Linux 系统上使用计时功能。我观察到
timeit.default_timer()
和 time.perf_counter()
在数值上给出相同的结果。
- 此外,在测量时间间隔的持续时间时,
timeit.default_timer()
、time.perf_counter()
和 time.time()
实际上都给出了相同的结果。所以这意味着这些函数中的任何一个都可以用来测量经过的时间或wall time 对于任何进程。
- 我想我还应该提到
time.time()
和其他人之间的区别在于它给出了 epoch 的当前时间(以秒为单位),它来自 1970 年 1 月 1 日 12:00AM
time.clock()
和time.process_time()
也给出相同的数值
time.process_time()
最适合测量 cpu 时间 因为 time.clock()
已在 python 中弃用 3
提供当前挂钟时间,time.time()
可用于get the epoch time.
为了提供经过的墙时间,time.perf_counter()
可以在操作的开始和结束时使用,结果的差异反映了经过的时间。结果不能用于给出绝对时间,如 the reference point is undefined. As mentioned in other answers, you can use timeit.default_time()
but this will always return time.perf_counter()
as of python 3.3
要提供经过的 CPU 时间,可以使用与 time.perf_counter()
类似的方式使用 time.process_time()
。这将 provide the sum of the system and user CPU time.
我正在编写 Python 脚本,该脚本将在命令行中 运行。这个想法是从用户那里得到一个命令,运行 然后提供挂钟时间和用户提供的命令的 CPU 时间。请参阅下面的代码。
#!/usr/bin/env python
import os
import sys
def execut_cmd(cmd_line):
utime = os.system('time '+cmd_line)
# Here I would like to store the wall-clock time in the Python variable
# utime.
cputime = os.system('time '+cmd_line)
# Here the CPU time in the cputime variable. utime and cputime are going to
# be used later in my Python script. In addition, I would like to silence the
# output of time in the screen.
execut_cmd(sys.argv[1])
print ('Your command wall-clock time is '+utime)
print ('Your command cpu time is '+ cputime)
我怎样才能做到这一点?另外,如果有比使用 'time' 更好的方法,我愿意尝试。
来自Python Documentation for wall time:
... On Windows, time.clock() has microsecond granularity, but time.time()’s granularity is 1/60th of a second. On Unix, time.clock() has 1/100th of a second granularity, and time.time() is much more precise. On either platform, default_timer() measures wall clock time, not the CPU time. This means that other processes running on the same computer may interfere with the timing.
对于挂钟时间,您可以使用 timeit.default_timer() 获取具有上述最佳粒度的计时器。
从 Python 3.3 及更高版本开始,您可以使用 time.process_time() 或 time.process_time_ns() 。以下是 process_time 方法的 the documentation entry:
Return the value (in fractional seconds) of the sum of the system and user CPU time of the current process. It does not include time elapsed during sleep. It is process-wide by definition. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.
我花了很少的时间在 Linux 系统上使用计时功能。我观察到
timeit.default_timer()
和time.perf_counter()
在数值上给出相同的结果。- 此外,在测量时间间隔的持续时间时,
timeit.default_timer()
、time.perf_counter()
和time.time()
实际上都给出了相同的结果。所以这意味着这些函数中的任何一个都可以用来测量经过的时间或wall time 对于任何进程。 - 我想我还应该提到
time.time()
和其他人之间的区别在于它给出了 epoch 的当前时间(以秒为单位),它来自 1970 年 1 月 1 日 12:00AM time.clock()
和time.process_time()
也给出相同的数值time.process_time()
最适合测量 cpu 时间 因为time.clock()
已在 python 中弃用 3
提供当前挂钟时间,time.time()
可用于get the epoch time.
为了提供经过的墙时间,time.perf_counter()
可以在操作的开始和结束时使用,结果的差异反映了经过的时间。结果不能用于给出绝对时间,如 the reference point is undefined. As mentioned in other answers, you can use timeit.default_time()
but this will always return time.perf_counter()
as of python 3.3
要提供经过的 CPU 时间,可以使用与 time.perf_counter()
类似的方式使用 time.process_time()
。这将 provide the sum of the system and user CPU time.