Keeping Track of Elapsed Time in Python
May 20, 2008 – 7:20 pmby Jake D
This article shows how to put a few lines of python code into your python script so you can tell how long the script has been running, or how long a certain part of the task took to run.
The Quick Answer: For the most accurrate time elapsed, use the time module and make 2 time.time() objects. The difference between these objects is the time elapsed. Do not use time.clock().
The Whole Story
Its pretty basic: I have certain parts of a large python script (that happen to access a MySQL database) that I would like to keep track of how long it took them to execute.
Wrong Answers
Initially, I read on a PLEAC-Python article, Dates and Times (which really is a great overview of Python’s time module), about some ways to use Python’s time module. That article suggests that all you need to do is:
#----------------------------- # High Resolution Timers t1 = time.clock() # Do Stuff Here t2 = time.clock() print t2 - t1 # 2.27236813618 # Accuracy will depend on platform and OS, # but time.clock() uses the most accurate timer it can
For one of my projects, that worked fine. But then I had a bigger script that used a lot of MySQL via the Python module MySQLdb, and I would look at my script’s run time after leaving, and the times looked short… but not too short. Eventually after running a script that took a 7 hour sleep and then some to run — but the script reported only taking an hour or so — I knew something was wrong.
Only timing the work of Python?
It seemed as if using the time.clock() approach was only timing what Python (as opposed to MySQL?) was doing. I created this test script
import time
yearstart = time.clock()
print yearstart
for x in range(0, 1000000):
z = x + 6
yearend = time.clock()
print yearend
elapsed= yearend-yearstart
min = elapsed/60
print elapsed, min
And I didn’t find a problem, but my script still did. I was wanting to time an event of a known length, and so I found out about the time.sleep() function from the aforementioned article. I incorporated the sleep() function:
import time yearstart = time.clock() print yearstart time.sleep(3) yearend = time.clock() print yearend elapsed= yearend-yearstart min = elapsed/60 print elapsed, min
and ran the script. You would expect to see 3 seconds as a result, or at least something close, but my output was:
0.03 0.03 0.0 0.0
That was funny (although I didn’t laugh) because yearend was not supposed to sample the clock() until after the sleep. I still don’t know why this does this, but it does. I am sure this has its uses, but this was not the use I was wanting.
Use time.time()
Still referencing the PLEAC-Python article, I tried using time.time(), which is supposed to just be the seconds since that day in 1970 and compared it to the time.clock() approach:
import time yearstart = time.clock() print yearstart time.sleep(3) yearend = time.clock() print yearend elapsed= yearend-yearstart min = elapsed/60 print elapsed, min yearstart = time.time() print yearstart time.sleep(3) yearend = time.time() print yearend elapsed= yearend-yearstart min = elapsed/60 print elapsed, min
And got the output:
0.03 0.03 0.0 0.0 1211338788.69 1211338791.69 3.00004386902 0.0500007311503
No Fluff Answer:
Read nothing else (on this page). This code will get you going:
import time start = time.time() # whatever you want to time, put between these two statements end = time.time() elapsed= end - start #if you want to convert to minutes, just divide min = elapsed/60 print "Your stuff took", elapsed, "seconds to run, which is the same as", min, "minutes"
So, this works for me. I supose that time.clock() does not reference absolute time, which was a problem for me. Hope this helps, or at least saves you some head-scratching.



8 Responses to “Keeping Track of Elapsed Time in Python”
I had exactly the same problem, only I think it also applies to the ftplib module…
Thank you so much!
Glad this helped, thanks for the comment!
My first thought would be to NOT use a continuous loop to check elapsed time, although it might depend on how many other things you are doing besides simply printing a statement.
The
timemodule has a function calledsleepwhich might be more efficient. Here is an example of usage: http://snipplr.com/view/5763/python-sleep/ . Read the blurb onsleepfrom the source: http://www.python.org/doc/2.5.2/lib/module-time.html .So, your code might look like:
#import the time module
import time
#sleep for 60 seconds
time.sleep(60)
#print a message
print 'Hello World!'
Thank You!I appreciate the time you took to write the script for me
Cheers
I used the time.sleep function in my code but i ran into a slight problem..i used time.sleep to execute some lines of code after 10 seconds.however i find that sometimes it takes less than 10 seconds to execute or more than 10 seconds.i used time.now to time when the lines of code were executed.I need as much precision n this time lapse as possible.is there a way to optimize time.sleep for precision?or another way for it to wait for a precise time before doing it what i want?your help has guided me in my script almost all the way.I would really appreciate a little help in this last piece!!
Thank You
With Best Regards
I was wrong in telling you to use sleep()… I should have known that because that is what this article is about.
Don’t use sleep, your first idea was correct.
You will have to forgive me, I am in a huge rush and I forget python syntax and loops at the moment, but here is some pseudo-code mixed with actual code:
import time
#
start = time.time()
#
# START YOUR LOOP HERE (there are different approaches to the loop that you could take, but here is a crude way)
# while 1 (< - you want the loop to go forever)
#
#
end = time.time()
#
elapsed= end - start
#
# Check to see if the time is up (units are already in seconds):
# if elapsed >= 10
# Then exit the loop and do what needs to be done
#
# END YOUR LOOP HERE
#
# Continue with your script
(It looks like I may have accidentally deleted your first comment!!! Sorry!)
Thank You for your time!!
Great tip. Quick, Easy and it works.