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

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

#<your script here>
#*******END***TIME***********
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.
Advertisements

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:

    @timeit
    def longRunningFunction():

    For more info see: http://gissolved.blogspot.com/2012/02/timing-functions-in-python.html

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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