Python Timeit Module

Source: Internet
Author: User

Test the execution time of a piece of code. In Python there is a very easy way to use the Timeit module. Super easy to use.

The following is a description of a function in a Timeit module


These are mainly the two functions:


1, Timeit (stmt= ' Pass ', setup= ' pass ', timer=<defaulttimer>, number=1000000)

Return:

Returns the time, in seconds, that the code used to run the stmt number of times. Float type

Number of references:

stmt: The piece of code to run

Setup: The preparation of running code, not counting the time, usually import or the like

Timer: This under Win32 is Time.clock (), Linux is Time.time (), default, no tube

Number: How many times to run stmt


2, repeat (stmt= ' pass ', setup= ' pass ', timer=<defaulttimer>, repeat=3, number=1000000)

This function is more than the Timeit function of a repeat, indicating how many times the process of running Timeit repeatedly. Returns a list of times that are run each time

Of course. For convenience, Python also uses a Timer class. The function inside the timer class is the same as the two functions described above.

Class Timeit. Timer (stmt= ' Pass ', setup= ' pass ', Timer=<timer function>)

Timer.timeit (number=1000000)

Timer.repeat (repeat=3,number=1000000)

Understand, the same, when using which kind of convenient O (∩_∩) o

is equivalent to


Timeit (stmt= ' Pass ', setup= ' pass ', timer=<defaulttimer>, number=1000000)

= Timer (stmt= ' Pass ', setup= ' pass ', timer=<timerfunction>). Timeit (number=1000000)

Repeat (stmt= ' pass ', setup= ' pass ', timer=<defaulttimer>, repeat=3, number=1000000)

= Timer (stmt= ' Pass ', setup= ' pass ', timer=<timerfunction>). Repeat (repeat=3, number=1000000)

Let's look at a simple example below.

Import Timeitimport mathimport pprintdef myfun (): For    i in range:        for J in range (2, 10):            

The output is:

0.05253329430442725[0.04811309220489818, 0.04411143942591811, 0.04442468209438866, 0.04363309734529072, 0.0596289993101522]0.05972538166968161[0.052578808196427296, 0.047218431876859035, 0.0431364604649378, 0.040855411289408616, 0.04188750572270261]0.04400300979614258


In order to output beautiful, the use of Pprint this interesting module



Python Timeit Module

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.