Skip to content

Instantly share code, notes, and snippets.

@fuzzmz
Created January 27, 2014 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuzzmz/8649063 to your computer and use it in GitHub Desktop.
Save fuzzmz/8649063 to your computer and use it in GitHub Desktop.
Measure the execution time of a python script
import atexit
from time import clock
def secondsToStr(t):
return "%d:%02d:%02d.%03d" % \
reduce(lambda ll,b : divmod(ll[0],b) + ll[1:],
[(t*1000,),1000,60,60])
line = "="*40
def log(s, elapsed=None):
print line
print secondsToStr(clock()), '-', s
if elapsed:
print "Elapsed time:", elapsed
print line
print
def endlog():
end = clock()
elapsed = end-start
log("End Program", secondsToStr(elapsed))
def now():
return secondsToStr(clock())
start = clock()
atexit.register(endlog)
log("Start Program")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment