Tracking time in a script – how I do it

Looking for an easy way to track time in a script? Here’s what I do.
import time
from time import strftime

starttime = time.time()
print '\n--> Starting script at ', strftime("%a, %d %b %Y %H:%M:%S")

#<your script here>
endtime = time.time() 
elapsed = endtime - starttime 
print '\n\n--> Finished script... ' , strftime("%a, %d %b %Y %H:%M:%S"), '\n    ..Elapsed time: ', round(elapsed/60,1), ' minutes.'

results in….

--> Starting script at Mon, 16 Jan 2012 08:16:50
--> Finished script... Mon, 16 Jan 2012 08:17:20
 ..Elapsed time: 0.5 minutes.

One thought on “Tracking time in a script – how I do it

  1. After seeing your post I decided to blog about the timing function I always use.
    The function goes like this :

    import time
    def timeit(method):
    def timed(*args, **kw):
    ts = time.time()
    result = method(*args, **kw)
    te = time.time()
    print(‘%r (%r, %r) %2.2f sec’ % (method.__name__, args, kw, te-ts))
    return result
    return timed

    You use it like this:

    def longRunningFunction():

    For more info see:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s