0
0
PythonHow-ToBeginner · 3 min read

How to Measure Execution Time in Python Quickly and Easily

You can measure execution time in Python using the time module by recording the start and end times with time.time(). For more precise timing, use the timeit module which runs code multiple times and gives an average duration.
📐

Syntax

To measure execution time with the time module, you record the start time before the code runs and the end time after it finishes. The difference gives the elapsed time.

Using timeit, you pass the code as a string or function to run it multiple times and get an average execution time.

python
import time

start = time.time()
# code to measure
end = time.time()
elapsed = end - start

import timeit
elapsed_time = timeit.timeit('code_to_test()', number=1000)
💻

Example

This example shows how to measure the time taken to sum numbers from 1 to 1 million using both time and timeit modules.

python
import time
import timeit

def sum_numbers():
    return sum(range(1, 1000001))

# 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, number=10)
print(f"Average time using timeit module over 10 runs: {elapsed / 10:.6f} seconds")
Output
Elapsed time using time module: 0.041234 seconds Average time using timeit module over 10 runs: 0.038567 seconds
⚠️

Common Pitfalls

One common mistake is measuring time with time.time() for very fast code, which can be inaccurate due to low resolution. Also, running the code only once may give misleading results because of background processes affecting timing.

Using timeit avoids these issues by running the code multiple times and providing a more reliable average.

python
import time

start = time.time()
for i in range(1000):
    pass
end = time.time()
print(f"Elapsed time (single run): {end - start} seconds")

import timeit
elapsed = timeit.timeit('for i in range(1000): pass', number=1000)
print(f"Elapsed time (1000 runs): {elapsed} seconds")
Output
Elapsed time (single run): 1.1920928955078125e-06 seconds Elapsed time (1000 runs): 0.000345678 seconds
📊

Quick Reference

Here is a quick summary of methods to measure execution time in Python:

MethodUsageBest For
time.time()Record start and end time around codeSimple timing for longer tasks
time.perf_counter()High resolution timer for start/endMore precise timing than time.time()
timeit.timeit()Run code multiple times and averageAccurate timing for small or fast code snippets

Key Takeaways

Use time.time() or time.perf_counter() to measure start and end times for simple timing.
For precise and reliable timing, use timeit to run code multiple times and get an average.
Avoid measuring very fast code with a single run using time.time() due to low accuracy.
timeit can accept functions or code strings and is best for benchmarking small code snippets.
Always consider running timing tests multiple times to reduce noise from background processes.