Python time-consuming efficiency statistics

Source: Internet
Author: User
Tags win32 virtual environment

Original address: http://www.cnblogs.com/moinmoin/archive/2011/03/18/python-runtime-measuring.html

The Python community has a saying: "Python has its own battery," don't write the timing frame yourself. Python 2.3 has a perfect timing tool called Timeit that can measure the running time of Python code.

The Timeit module Timeit module defines a Timer class that accepts two parameters. All two parameters are strings. The first argument is the statement or function you want to tick. The second parameter passed to the Timer is the import statement that builds the environment for the first parameter statement. Internally, Timeit builds an independent virtual environment, executes the statement manually, and then compiles and executes the timed statements manually. Once you have a timer object, the simplest thing to do is call Timeit (), which takes a parameter to call the timed statement for each test, defaults to 1 million times, and returns the number of seconds spent. Another major method of the Timer object is repeat (), which accepts two optional parameters. The first parameter is the number of times the entire test is repeated, and the second is the number of times the time statement is invoked in each test. All two parameters are optional, and their default values are 3 and 1000000, respectively. The repeat () method returns a time-consuming list of each test loop recorded in seconds. Python has a handy min function to return the input list to a minimum value, such as: Min (t.repeat (3, 1000000)) You can use the Timeit module at the command line to test an existing Python program without having to modify the code. Refer to Documentation: 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 to 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 results:

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 practice only, not recommended). Time.localtime (), Time.time (), Time.clock () Contrast: Ime.localtime (), localtime return is struct_time, including month and year, obviously not necessary, More importantly, the precision of the localtime () is dependent upon time () Time.time (), which returns UTC (seconds since the 00:00:00 UTC on January 1). In many systems, including windows, the precision is poor, and the accuracy under Win32 is only 1/18.2 seconds. However, under the Unix/linux system, time () precision is still very high. The Python standard Library Manual recommends that you use Time.clock () as much as possible under any system. Note, however, that under the Win32 system, this function returns the real time (wall time), while the CPU is returned under Unix/linux. Under Win32, the time resolution of this function is better than 1 microseconds.

Cases:

#-*-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

Results:

1.749e-05

Other methods

With complex programs, there are a number of profiling tools available. For example, profile in the Python standard library can count the running time of each function in the program and provide a variety of reports. (Don't know, write it down first)

Reference: Http://docs.python.org/library/timeit.html http://woodpecker.org.cn/diveintopython/performance_tuning/ Timeit.html http://hi.baidu.com/shanyaodan0880/blog/item/b446617b7a98a5e42e73b3f2.html

Related Article

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.