Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Timeit in Python with Examples?
Python provides the timeit module for precise timing measurements of small code snippets. Unlike the basic time module, timeit accounts for background processes and provides more accurate performance measurements.
What is Python timeit?
The timeit module runs code approximately 1 million times (default value) and measures the minimum execution time. This approach eliminates timing variations caused by background processes and system overhead.
Using timeit from Command Line
You can use timeit directly from the command line interface. The module automatically decides the number of repetitions ?
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
Using timeit in Python Scripts
Basic Example
Import the timeit module and use timeit.timeit() to measure execution time ?
import timeit
# Compare string multiplication vs concatenation
time1 = timeit.timeit("y = 'x' * 5", number=1000000)
time2 = timeit.timeit("xy = 'x' + 'x' + 'x' + 'x' + 'x'", number=1000000)
print(f"String multiplication: {time1:.4f} seconds")
print(f"String concatenation: {time2:.4f} seconds")
String multiplication: 0.0734 seconds String concatenation: 0.0712 seconds
Using repeat() for Multiple Runs
The repeat() function runs the timing multiple times and returns a list of results ?
import timeit
# Run timing 5 times for better accuracy
results1 = timeit.repeat("y = 'x' * 3", number=1000000, repeat=5)
results2 = timeit.repeat("xy = 'x' + 'x' + 'x'", number=1000000, repeat=5)
print(f"String multiplication times: {results1}")
print(f"Best time: {min(results1):.4f} seconds")
print()
print(f"String concatenation times: {results2}")
print(f"Best time: {min(results2):.4f} seconds")
String multiplication times: [0.07303, 0.07213, 0.07362, 0.07293, 0.07278] Best time: 0.0721 seconds String concatenation times: [0.07388, 0.07378, 0.09486, 0.07352, 0.07398] Best time: 0.0735 seconds
Testing Multiple Statements
Use semicolons to separate multiple statements in a single timing test ?
import timeit
# Compare different arithmetic operations
time1 = timeit.timeit("x = 2; x *= 2", number=10000000)
time2 = timeit.timeit("x = 1; x *= 4", number=10000000)
print(f"Method 1 (2 * 2): {time1:.4f} seconds")
print(f"Method 2 (1 * 4): {time2:.4f} seconds")
Method 1 (2 * 2): 2.4859 seconds Method 2 (1 * 4): 2.2706 seconds
Testing Custom Functions
Use the setup parameter to import custom functions for timing ?
import timeit
def func1():
return 1
def func2():
return sum([-1, 0, 1, 1])
# Test the functions
print(f"func1() result: {func1()}")
print(f"func2() result: {func2()}")
# Time the functions using setup parameter
time1 = timeit.timeit("func1()", setup="from __main__ import func1", number=1000000)
time2 = timeit.timeit("func2()", setup="from __main__ import func2", number=1000000)
print(f"func1() time: {time1:.4f} seconds")
print(f"func2() time: {time2:.4f} seconds")
func1() result: 1 func2() result: 1 func1() time: 0.4479 seconds func2() time: 1.5836 seconds
Key Parameters
| Parameter | Description | Default |
|---|---|---|
stmt |
Statement to be timed | Required |
number |
Number of executions | 1,000,000 |
repeat |
Number of timing runs | 3 |
setup |
Setup code (imports, etc.) | None |
Conclusion
The timeit module provides precise performance measurements by running code multiple times and accounting for system overhead. Use repeat() for multiple runs and the setup parameter for custom functions.
