Keeping Track of Elapsed Time in Python

refer to: http://www.techarticles.zeromu.net/programming/keeping-track-of-elapsed-time-in-python/


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:

?
1
2
3
4
5
6
7
8
9
10
11
#-----------------------------
# 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.


你可能感兴趣的:(python)