简体   繁体   中英

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. 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. 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 :

... 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.

For wall time you can use timeit.default_timer() which gets the timer with best granularity described above.

From Python 3.3 and above you can use time.process_time() or time.process_time_ns() . Below is the documentation entry for process_time method:

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.

To provide the current wall time, time.time() can be used to get the epoch 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. 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

To provide the elapsed CPU time, time.process_time() can be used in a similar manner to time.perf_counter() . This will provide the sum of the system and user CPU time.

With the little time I have spent using the timing functions on Linux systems. I have observed that

  1. timeit.default_timer() and time.perf_counter() numerically gives the same result.
  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. 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
  4. time.clock() and time.process_time() also gives the same numerical value
  5. time.process_time() is most suitable for measuring the cpu time since time.clock() is already deprecated in python 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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