How to Measure Execution Time of Function in Python Easily
You can measure the execution time of a function in Python using the
time module by recording the time before and after the function runs. Alternatively, use the timeit module for more precise timing, especially for small or fast functions.Syntax
To measure execution time using the time module, you record the start time before the function call and the end time after it finishes. The difference gives the elapsed time.
Using timeit, you pass the function call as a string or a callable to measure its execution time accurately over multiple runs.
python
import time start_time = time.time() # call your function here end_time = time.time() elapsed_time = end_time - start_time # Using timeit import timeit elapsed_time = timeit.timeit('your_function()', globals=globals(), number=1000)
Example
This example shows how to measure the time taken by a simple function that sums numbers from 1 to 1 million using both time and timeit modules.
python
import time import timeit def sum_numbers(): total = 0 for i in range(1, 1000001): total += i return total # Using time module start = time.time() sum_numbers() end = time.time() print(f"Elapsed time using time module: {end - start:.6f} seconds") # Using timeit module elapsed = timeit.timeit('sum_numbers()', globals=globals(), number=10) print(f"Elapsed time using timeit module (10 runs): {elapsed:.6f} seconds")
Output
Elapsed time using time module: 0.080123 seconds
Elapsed time using timeit module (10 runs): 0.750456 seconds
Common Pitfalls
- Using
time.time()can be affected by system clock changes; prefertime.perf_counter()for higher precision and monotonic timing. - Measuring very fast functions once may give inaccurate results; use
timeitto run multiple iterations. - Passing the function call as a string to
timeit.timeit()requires careful use ofglobals=globals()to access the function.
python
import time def fast_function(): return sum(range(10)) # Wrong: single run with time.time() may be too fast to measure start = time.time() fast_function() end = time.time() print(f"Single run time: {end - start}") # Right: use timeit for multiple runs import timeit elapsed = timeit.timeit('fast_function()', globals=globals(), number=10000) print(f"Multiple runs time: {elapsed}")
Output
Single run time: 1.1920928955078125e-06
Multiple runs time: 0.004321
Quick Reference
| Method | Usage | Best For |
|---|---|---|
| time.time() | Record start and end time around function call | Simple timing, less precise |
| time.perf_counter() | High precision timer for elapsed time | More accurate timing, monotonic clock |
| timeit.timeit() | Run function multiple times and average | Small or fast functions, benchmarking |
Key Takeaways
Use time.perf_counter() for precise and reliable timing in Python.
For fast or small functions, use timeit to run multiple iterations for accuracy.
Avoid measuring execution time with a single run for very quick functions.
Pass globals=globals() to timeit.timeit() when timing functions defined in your script.
Subtract start time from end time to get elapsed execution time.