Measure the time when Python code runs.

Source: Internet
Author: User

The Python community has a saying: "python has its own battery." Don't write the timing framework yourself. Python 2.3 hasTimeitThe perfect timing tool can measure the running time of python code.

Timeit Module

  • TimeitThe module definesTimerClass. Both parameters are strings. The first parameter is the statement or function for timing. PassTimerThe second parameter is the import statement used to build the environment for the first parameter statement. Internally,TimeitBuild an independent virtual environment, manually execute the created statement, and then manually compile and execute the timed statement.
  • OnceTimerObject, the simplest thing is to callTimeit ()It accepts a parameter as the number of times the timer statement is called in each test. The default value is 1 million. The returned time is in seconds.
  • TimerAnother main method of the object isRepeat ()It accepts two optional parameters. The first parameter is the number of times the entire test is repeated, and the second parameter is the number of times the timing statement is called in each test. Both parameters are optional and Their default values are3And1000000.Repeat ()Method returns the time consumed for each test loop recorded in secondsList. Python has a convenientMinThe function returns the input list to the minimum value, for example: Min (t. repeat (3, 1000000 ))
  • You can useTimeitModule to test an existing Python program without modifying the code.

  • For more information, see:
    Http://docs.python.org/library/timeit.html

Example:

# -*- coding: utf-8 -*-
#!/bin/env python

def test1():
n=0
for i in range(101):
n+=i
return n

def test2():
return sum(range(101))

def test3():
return sum(x for x in range(101))

if __name__=='__main__':
from timeit import Timer
t1=Timer("test1()","from __main__ import test1")
t2=Timer("test2()","from __main__ import test2")
t3=Timer("test3()","from __main__ import test3")
print t1.timeit(1000000)
print t2.timeit(1000000)
print t3.timeit(1000000)
print t1.repeat(3,1000000)
print t2.repeat(3,1000000)
print t3.repeat(3,1000000)

Execution result:

tiny@tiny-desktop:~/workspace/py$ python timetest.py 
7.99498915672
3.13702893257
10.6419789791[8.2126381397247314, 8.6312708854675293, 8.6079621315002441][3.3426268100738525, 3.3914170265197754, 3.5281510353088379][11.097387075424194, 10.941920042037964, 10.874698877334595]

Use the time module

Use the time module (for exercises only, not recommended ).


Time. localtime (),


Time. time (),


Time. clock () comparison:

  • Ime. localtime (). localtime returns struct_time, which includes the year, month, and day. It is obviously unnecessary. More importantly, the accuracy of localtime () depends on time ()
  • Time. time (), time returns UTC time (seconds since the 00:00:00 UTC on January 1 ). In many systems, including windows, the accuracy is very poor, while in win32, the accuracy is only 1/18. 2 seconds. However, in Unix/Linux systems, time () is very accurate.
  • Time. clock () is recommended in any system (). However, in the win32 system, this function returns the real time (wall time), while in Unix/Linux, it returns the CPU time. In win32, the time resolution of this function is better than 1 microsecond.

Example:

# -*- coding: utf-8 -*-
#!/bin/env python

def test():
L=[]
for i in range(100):
L.append(i)

if __name__=='__main__':
from time import clock
start=clock()
for i in range(1000000):
test()
finish=clock()
print (finish-start)/1000000

Result:

1.749e-05

Other methods


Many performance analysis tools are available for complex programs. For example, the profile in the standard library of python can calculate the running time of each function in the program, and provides a variety of reports. (Skip this step)

Refer:

  1. Http://docs.python.org/library/timeit.html
  2. Http://woodpecker.org.cn/diveintopython/performance_tuning/timeit.html
  3. Http://hi.baidu.com/shanyaodan0880/blog/item/b446617b7a98a5e42e73b3f2.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.