简体   繁体   English

如何使用 bash 内置“时间”在 Python 变量中捕获挂钟时间和 CPU 时间?

[英]How to capture wall-clock time and CPU time in a Python variable using bash builtin 'time'?

I am working on a Python script that is going to be run in the command line.我正在处理将在命令行中运行的 Python 脚本。 The idea is to get a command from the user, run it and then provide the wall-clock time and the CPU time of the command provided by the user.这个想法是从用户那里获取命令,运行它,然后提供挂钟时间和用户提供的命令的 CPU 时间。 See code below.请参阅下面的代码。

#!/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)

How can I accomplish this?我怎样才能做到这一点? Also, if there is a better method than using 'time' I am open to try it.此外,如果有比使用“时间”更好的方法,我愿意尝试。

From Python Documentation for wall time :来自墙上时间的 Python 文档

... On Windows, time.clock() has microsecond granularity, but time.time()'s granularity is 1/60th of a second. ... 在 Windows 上,time.clock() 的粒度为微秒,但 time.time() 的粒度为 1/60 秒。 On Unix, time.clock() has 1/100th of a second granularity, and time.time() is much more precise.在 Unix 上,time.clock() 具有 1/100 秒的粒度,而 time.time() 精确得多。 On either platform, default_timer() measures wall clock time, not the CPU time.在任一平台上,default_timer() 测量挂钟时间,而不是 CPU 时间。 This means that other processes running on the same computer may interfere with the timing.这意味着在同一台计算机上运行的其他进程可能会干扰计时。

For wall time you can use timeit.default_timer() which gets the timer with best granularity described above.对于挂钟时间,您可以使用 timeit.default_timer() 获取具有上述最佳粒度的计时器。

From Python 3.3 and above you can use time.process_time() or time.process_time_ns() .从 Python 3.3 及更高版本开始,您可以使用 time.process_time() 或 time.process_time_ns() 。 Below is the documentation entry for process_time method:以下是 process_time 方法 的文档条目

Return the value (in fractional seconds) of the sum of the system and user CPU time of the current process.返回当前进程的系统和用户 CPU 时间总和的值(以秒为单位)。 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.返回值的参考点未定义,因此只有连续调用结果之间的差异才有效。

To provide the current wall time, time.time() can be used to get the epoch time.要提供当前的挂钟时间,可以使用time.time()获取纪元时间。

To provide the elapsed wall time, time.perf_counter() can be used at the start and end of the operation with the difference in results reflecting the elapsed time.为了提供经过的墙时间,可以在操作的开始和结束时使用time.perf_counter() ,结果的差异反映了经过的时间。 The results cannot be used to give an absolute time as 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正如其他答案中提到的,您可以使用timeit.default_time()但这将始终返回time.perf_counter()作为 python 3.3

To provide the elapsed CPU time, time.process_time() can be used in a similar manner to time.perf_counter() .要提供经过的 CPU 时间,可以以与 time.perf_counter( time.process_time()类似的方式使用time.perf_counter() This will provide the sum of the system and user CPU time.这将 提供系统和用户 CPU 时间的总和。

With the little time I have spent using the timing functions on Linux systems.我花了很少的时间在 Linux 系统上使用计时功能。 I have observed that我观察到

  1. timeit.default_timer() and time.perf_counter() numerically gives the same result. timeit.default_timer()time.perf_counter()数值上给出相同的结果。
  2. Also, when measuring the duration of a time interval, timeit.default_timer() , time.perf_counter() and time.time() all virtually gives the same result.此外,在测量时间间隔的持续时间时, timeit.default_timer()time.perf_counter()time.time()几乎都给出了相同的结果。 So this means that any of these functions can be used to measure the elapsed time or wall time for any process.因此,这意味着这些函数中的任何一个都可用于测量任何过程的经过时间壁垒时间
  3. I think I should also mention that the difference between time.time() and others is that it gives the current time in seconds from epoch which is from 1 January 1970 12:00AM我想我还应该提到time.time()和其他人之间的区别在于它给出了从1970 年 1 月 1 日上午 12:00开始的纪元的当前时间(以秒为单位)
  4. time.clock() and time.process_time() also gives the same numerical value time.clock()time.process_time()也给出了相同的数值
  5. time.process_time() is most suitable for measuring the cpu time since time.clock() is already deprecated in python 3 time.process_time()最适合测量CPU 时间,因为time.clock()在 python 3 中已经被弃用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM