python-3.xbashtimecpu-timewall-time

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.


Solution

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