- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Timeit in Python with Examples?
Python provides many ways to measure the time of execution for a piece of python code. One way is to use the python inbuilt time module and save the time before and after the execution of the program?
When some program is running, many processes also run in the background to make that code executable. The time module doesn’t count background processes execution time, however if you need precise time performance measurements timeit is the module to go for it.
The timeit module runs the code approximately 1 million times (default value) and take into account the minimum amount of time it took to run that piece of code.
Getting Python Execution time using timeit
We can use timeit module in many ways. One of the simplest ways is to use directly on the python CLI.
We will start using python CLI with timeit module first. When CLI is used, we’ll notice that the module itself decides the number of repetitions performed for the same piece of code.
C:\Users\rajesh>python -m timeit "'-'.join(str(n) for n in range(200))" 1000 loops, best of 3: 290 usec per loop C:\Users\rajesh>python -m timeit "'-'.join(str(n) for n in range(200))" 1000 loops, best of 3: 292 usec per loop C:\Users\rajesh>python -m timeit "'-'.join(str(n) for n in range(200))" 1000 loops, best of 3: 294 usec per loop
Next we introduce timeit with another simple example but first we must import timeit module with “import timeit” statement. This is required incase we are not using command-line syntax like above.
#Import timeit module import timeit # The instructions being timed. print('x' * 5) print('x' + 'x' + 'x' + 'x' + 'x') # Call timeit on the statements and print the time returned. # ... Specify optional number of iterations. print(timeit.timeit("y = 'x' * 3", number=10000000)) print(timeit.timeit("xy = 'x' + 'x' + 'x' + 'x' + 'x'", number = 10000000))
Above we pass the statements in quoted strings to the timeit.timeit method and then we increase the iterations by specifying a number argument.
First time on running above program, output generated:
xxxxx xxxxx 0.9041136896626635 0.7712796073957123
Second time on running above program, output generated:
xxxxx xxxxx 0.7317015874427751 0.7312688195585995
Third time on running above program, output generated:
xxxxx xxxxx 0.7240862411172824 0.7255863890794246
We performed our above program multiple times(3 times) and see there is reduction is execution time. One instead of doing it manually, let’s do the repetition through program:
#Import timeit module import timeit # Call timeit on the statements and print the time returned. # ... Specify optional number of iterations. print(timeit.repeat("y = 'x' * 3", number=10000000, repeat = 5)) print() print(timeit.repeat("xy= 'x' + 'x' + 'x' + 'x' + 'x'", number = 10000000, repeat = 5))
[0.7303736343436382, 0.7213687552991258, 0.7362311105941466, 0.7293136666273243, 0.7278277732068212] [0.7388334197158559, 0.7378481457977326, 0.9486733868277772, 0.735295442480929, 0.7398226849056382]
Running Multiple statements using timeit module:
We can use multiple statements with timeit module. We separate each statements using a semicolon. Though it’s not the best way to write code but helps in specifying longer code fragments.
#Import timeit module import timeit # Use semicolon for multiple statements. print(timeit.repeat("x = 2; x *= 2", number=100000000)) print(timeit.repeat("x = 1; x *= 4", number=100000000))
[24.859605879029118, 23.58795536845994, 23.95826726353284] [22.70639977603264, 21.380195994245724, 20.71523588130414]
Using Methods, setup in Timeit module:
We can use the custom methods in timeit by specifying a setup argument. In this argument, we specify an import statement that indicates the methods we invoke.
#Import timeit module import timeit def func1(): return 1 def func2(): return sum([-1, 0, 1, 1]) # Test methods. print(func1()) print(func2()) # Pass setup argument to call methods. print(timeit.repeat("func1()", setup="from __main__ import func1")) print(timeit.repeat("func2()", setup="from __main__ import func2"))
In above program we benchmark the func1() method against the func2() method.
1 1 [0.44798489246658874, 0.4411512652046069, 0.44570416580426686] [1.583622557983199, 1.5712399227517881, 1.5469479030713984]
As func1() is doing less work, executed much faster.
Above we saw how we can measure the performance of small piece of python code using the timeit module using CLI and scripts as well.
- Measure execution time of small Python code snippets (timeit)
- Regular Expression in Python with Examples?
- Explain Stack in Python with examples
- Explain Python Matrix with examples
- User-defined Exceptions in Python with Examples
- What is Queue in Python? Explain with examples
- What is an object in Python? Explain with examples
- Important differences between Python 2.x and Python 3.x with examples
- Regular Expression Examples in Python
- Stack with Examples in C#
- Queue with Examples in C#?
- SortedList with Examples in C#?
- negative_binomial_distribution in C++ with Examples
- Popovers in Bootstrap with examples
- ratio_not_equal() in C++ with examples