Difference between revisions of "CSC352 Scripts to Measure Execution Times"
Line 19: | Line 19: | ||
We use a Python script below that will run the program '''randomTime.py''' above the right number of times and keep track of the best time. | We use a Python script below that will run the program '''randomTime.py''' above the right number of times and keep track of the best time. | ||
− | <font color="red">'''Important Note''': Depending on whether you are running this script on a Linux box (e.g. beowulf) or on a Mac, you should set the two variables '''MAC''' and '''LINUX''' | + | <font color="red">'''Important Note''': Depending on whether you are running this script on a Linux box (e.g. beowulf) or on a Mac, you should set the two variables '''MAC''' and '''LINUX''' '''True''' or '''False''' accordingly.</font> |
Setting for a Linux box: | Setting for a Linux box: |
Latest revision as of 21:18, 21 February 2010
Assume that we want to measure the execution time of this program:
.
#! /usr/bin/python
# randomTime.py
# D. Thiebaut
import random
import time
time.sleep( random.randrange( 2 ) )
.
which takes a random number of time between 0 and 2 seconds.
We use a Python script below that will run the program randomTime.py above the right number of times and keep track of the best time.
Important Note: Depending on whether you are running this script on a Linux box (e.g. beowulf) or on a Mac, you should set the two variables MAC and LINUX True or False accordingly.
Setting for a Linux box:
LINUX = True MAC = False
Setting for a Mac:
LINUX = False MAC = True
.
#! /usr/bin/python
# timeIt.py
# D. Thiebaut
#
# Note: there are two different version of the command "time". One is internal
# to bash, the other is located in /usr/bin/time. Depending on how your path
# is setup, it is possible that when you execute "time" at the command line,
# it is the bash command that runs, but when you execute "time" inside a script,
# (like this python program), it is the /usr/bin/time command that runs.
# Moreover, the output of /usr/bin/time on Linux is not the same as /usr/bin/time
# on Mac OS X.
#
# For this reason, set the MAC and LINUX variables below to True or False
# depending on where you run your program!
#
# on Linux system, the /usr/bin/time command returns a string of the type
#
# 0.01user 0.00system 0:01.08elapsed 1%CPU (0avgtext+0avgdata 0maxresident)k
#
# on Mac OS X, the /usr/bin/time command returns a string of the type
#
# 0.03 real 0.01 user 0.01 sys
#
from subprocess import Popen, PIPE
MAC=False
LINUX=True
def oldRunCommandOnce( program ):
command="(time ./%s) 2>&1 | grep real | cut -d'm' -f 2 | cut -d's' -f 1" % program
p = Popen( command, shell=True, stdin=PIPE,
stdout=PIPE, close_fds=True )
fin, fout = p.stdin, p.stdout
execTime = float( fout.readline() )
return execTime
def runCommandOnce( program ):
if MAC:
command="(/usr/bin/time ./%s) 2>&1 | grep real" % program
elif LINUX:
command="(/usr/bin/time ./%s) 2>&1 | grep elapsed" % program
else:
print "You must set MAC and LINUX to be True and/or False!"
return
p = Popen( command, shell=True, stdin=PIPE,
stdout=PIPE, close_fds=True )
fin, fout = p.stdin, p.stdout
line = fout.readline()
if MAC:
words = line.split()
execTime = float( words[0] )
return execTime
if LINUX:
# 0.01user 0.00system 0:01.08elapsed 1%CPU (0avgtext+0avgdata 0maxresident)k
execTime = float( line.split()[0].split( 'u' )[0] )
return execTime
def main():
bestTime = 1E10
program = "randomTime.py"
for i in range( 3 ):
execTime = runCommandOnce( program )
print "sample %d: %f sec" % ( i+1, execTime )
bestTime = min( bestTime, execTime )
print "Best time = %f sec" % bestTime
main()
.