2012年2月1日 星期三

python 用time模組,timeit模組 測量python function 執行的時間

# -*- coding: utf-8 -*-
import time
tStart = time.time()#計時開始
 
#模擬要測量的function
time.sleep(2)
print "abc"
for x in range(1000):
    x += 1
    print x
#end of 模擬要測量的function
 
tEnd = time.time()#計時結束
#列印結果
print "It cost %f sec" % (tEnd - tStart)#會自動做近位
print tEnd - tStart#原型長這樣


Let's timeit!

Every now and then you might want to time snippets just to make sure that you choose the more efficient solution. In those cases you can use the timeit module to measure execution time for snippets. Introduction It's very easy to setup and measure the execution time for a snippet with timeit. The module contains a class, Timer, which is used to perform the measurement. The class has one constructor and three methods:
  • Timer([stmt='pass'[, setup='pass'[, timer=<timer function>]]]) - stmt is the statement to be timed and setup is called once before executing the main statement. A timer function can be specified and default is time.time() for all platforms but windows which is set to time.clock() instead (according to my timeit.py)
  • timeit([number=1000000]) - Executes the main statement passed to the constructornumber of times and returns the result in seconds as a float. 
  • repeat([repeat=3[, number=1000000]]) - Convenience function that callstimeit(number) repeat times. Returns a list with the results.
  • print_exc([file=None]) - Helper to print a traceback from the timed snippet.
Starting with Python 2.6 the timeit module also defines two convenience functions,timeit.timeit() and timeit.repeat(). They are basically wrappers around the Timer class. Example Suppose that I would like to create a list containing 100 'c':s like this ['c', 'c', ...]. There are at least two ways of doing this:
  1. lst = ['c'] * 100  
  2. # or  
  3. lst = ['c' for i in xrange(100)]  
Which one should I choose? Well, let's execute both statements with timeit and measure the execution time.
  1. >>> import timeit  
  2. >>> t = timeit.Timer(stmt="lst = ['c'] * 100")  
  3. >>> print t.timeit()  
  4. 1.10580182076  
  5. >>> t = timeit.Timer(stmt="lst = ['c' for x in xrange(100)]")  
  6. >>> print t.timeit()  
  7. 7.66900897026  
Ok, I think I'll stick with the first snippet :) The result returned is the total execution time in seconds. In this particular case when we are executing the snippet 1000000 times the result is also the execution time in microseconds for one single pass (1000000*exe_time/1000000 == exe_time). Normally, the timeit module doesn't have access to things that you have defined in your module. If you would like to measure a function that you have defined in your module you can specify the import statement in the setup parameter:
  1. >>> def create_lst(size):  
  2. ...    return ['c'] * size  
  3. ...  
  4. >>> t = timeit.Timer(stmt="create_lst(100)", setup="from __main__ import create_lst")  
  5. >>> print t.timeit()  
  6. 1.21339488029  
This will introduce a little overhead since the create_lst() function is called in the measurement loop instead of just executing an inlined snippet.
Note: Timer.timeit() will by default disable garbage collection during timing. To enable GC you can pass 'gc.enable()' as a setup statement.
I find the timeit module as a simple and convenient way to measure execution time for small snippets.