- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Measure execution time of small Python code snippets (timeit)
The Timer class and other convenience functions in timeit module of Python's standard library are designed to provide a mechanism to measure time taken by small bits of Python code to execute. The module has a command line interface and the functions can be called from within program as well.
Easiest way to measure time of execution is by using following convenience function
This function returns object of Timer class. It mainly requires two parameters.
stmt − a string containing valid Python statement whose execution time is to be measured.
setup − a string containing Python statement which will be executed once, primarily to initialize certain objects or variables.
Both strings may contains multiple statements separated by semi colon(;) or newlines, and both default to pass keyword. Another optional parameter number may be given which denotes number of executions of 'stmt'
In following example time required to perform thousand times the cumulative addition of numbers in a range 0-100 is measured.
import timeit setupcode = "s = 0" function = ''' for x in range(100): s = s + x ''' print (timeit.timeit(setup = setupcode, stmt = function, number = 1000))
Here a string contains a for loop within which numbers within range 0-100 are added. This string is the stmt parameter. For addition, the initialization of a variable is done by a setupcode string. The timeit() function calculates the time required in seconds.
The same result can be obtained by first creating a Timer object and then execute timeit() method on it.
The Timer class also has a repeat() method to call timeit() repeatedly. It returns a list of all calls.
An object oriented version of the above code is as follows −
import timeit setupcode = "s = 0" function = ''' for x in range(100): s = s + x ''' t = timeit.Timer(setup = setupcode, stmt = function) print (t.timeit(number = 1000)) print ('calling repeat() :',t.repeat(3,1000))
0.019971274194651528 calling repeat() : [0.023369810546474253, 0.020518432391765262, 0.02075439436427058]
Command line interface
As mentioned earlier, timeit module has a command line interface. The module itself is imported using –m option in the command line for execution of Python script. Following command line options define various parameter line setup and executable code, repeat frequency, etc.
|-n N, --number = N||how many times to execute ‘statement’|
|-r N, --repeat = N||how many times to repeat the timer (default 3)|
|-s S, --setup = S||statement to be executed once initially (default pass)|
|-v, --verbose||print raw timing results; repeat for more digits precision|
|-h, --help||print a short usage message and exit|
Following is the command line equivalent of example code used earlier −
C:\Users\acer>python -m timeit -s "s = 0" "for x in range(100): s = s + x" 10000 loops, best of 3: 22.4 usec per loop
- How to measure the execution time in Golang?
- Measure execution time with high precision in C/C++
- How to measure execution time for a Java method?
- How to calculate Execution Time of a Code Snippet in C++?
- How to measure elapsed time in python?
- Timeit in Python with Examples?
- How to get the timing Execution Speed of Python Code?
- How to measure time with high-precision in Python?
- How to measure actual MySQL query time?
- How to measure elapsed time in Java?
- Understanding blocking and unblocking of code execution in Node
- How do I get time of a Python program's execution?
- How to find PHP execution time?
- Calculate the execution time of a method in C#
- Java Program to Calculate the Execution Time of Methods