Python – How to use the bash time function from python

bashlinuxpythontimeunix

I would like to use python to make system calls to programs and time them. From the Linux command line if you type:

$ time prog args

You get something along the lines of:

real    0m0.110s
user    0m0.060s
sys     0m0.024s

if you do a 'man time', it states that you can type:

$ time -f "%E" prog args

in order to format to get only display the elapsed time. This does not work in the shell. I beleive it doesn't work because bash must have its own time function. If you type:

$/usr/bin/time -f "%E" prog args

you get the expected output as stated in the man pages.

I want the bash time because it seems to be more accurate. My problem is, when you try to use the 'time prog args' command from python, it doesn't behave the same way as when you type it in.

Code below to see what I'm doing (Please excuse the sloppiness):

#!/usr/bin/env python
"""Quick & Dirty script to time program execution for multiple inputs.
Saves the results as a .csv for importing into excel."""
import subprocess
timing = 'time -f "%E" '
program = 'java smartfib '
filename = 'smarttimes.csv'
#arglist = [0, 10, 20, 30, 31, 32, 33, 34, 35]
#arglist = [0, 2, 3, 5]
arglist = range(50)
timelist = list()

#run the program with the specified input values
for arg in arglist:

    cmd = timing + program + str(arg)
    pipe = subprocess.Popen(cmd, shell = True, bufsize = 256,
                stderr = subprocess.PIPE, stdout = subprocess.PIPE)

    [answer,times] = pipe.communicate() #read from pipe
    #print times
    if(int(answer) < 0):
        print "overflow occured at fib(%d)!\n" %arg
        break
    #save (n, [time it takes to complete] as a tuple in a list)
    timelist.append([arg,times])


csv = list() #list for formatted comma separated values for excel
for item in range(len(timelist)):
    csv.append(
        str(timelist[item][0]) + "," + timelist[item][1][2:].strip('\n')
        )

results = file(filename,'w')
results.write('n,time(in s)\n')
for item in range(len(csv)):
    results.write(csv[item]+'\n')

results.close()

def getTimeInSeconds(self, timestring):
    pass

Best Solution

You are correct that bash has it's own version of time.

$ type time
time is a shell keyword

Perhaps you could explicitly invoke bash with the -c option to get it's timing.

Depending on which distribution you're using, the default shell may be dash, a simpler shell that doesn't have time as a keyword. Both Debian and Ubuntu use dash but I don't know what Red Hat, Fedora and others use.