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.


Before Playstation, there was Pong, at one time the ultimate in video game entertainment. For those of you not familiar with this game please refer to the Wikipedia entry (http://en.wikipedia.org/wiki/Pong) and the many fine websites extolling the game and its virtues. Pong is not so very different in structure from the Billiard ball simulation that you developed earlier in the course. They both involve a ball moving and colliding with obstacles. The difference in this case is that two of the obstacles are under user control. The goal of this project is to develop your own version of Pong in MATLAB using the keyboard as input, for example, one player could move the left paddle up and down using the q and a keys while the right paddle is controlled with the p and l keys. You may check the code for the Lunarlander game which demonstrates some of the techniques you can use to capture user input. You will also probably find the plot, set, line and text commands useful in your program. You have used most of these before in the billiard simulation and you can use Matlabs online help to get more details on the many options these functions offer. Your program should allow you to play a game to 11 keeping track of score appropriately. The general structure of the code is outlined below in pseudo code While not done Update Ball State (position and velocity) taking into account collisions with walls and paddles Check for scoring and handle appropriately Update Display Note that in this case it is implicitly assumed that capturing the user input and moving the paddles is being handled with callback functions which removes that functionality from the main loop. For extra credit you could consider adding extra features like spin or gravity to the ball flight or providing a single player mode where the computer controls one of the paddles.
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值