Understanding timeit Output in Python 2026 with Efficient Code
The output of timeit can look confusing at first. In 2026, with faster Python interpreters and free-threading, correctly interpreting timeit results is essential for making accurate performance decisions and avoiding common benchmarking mistakes.
This March 15, 2026 guide explains exactly what every part of timeit output means and how to use it effectively for efficient code optimization.
TL;DR — Key Takeaways 2026
timeitshows the best time from multiple runs, not the average- The number after "loops" is how many times the code ran per measurement
- Lower time = better performance
- Use
repeat+minfor reliable results - Always compare relative improvement, not absolute microseconds
1. Typical timeit Output Explained
import timeit
result = timeit.repeat( # repeat() runs the timing 5 times; timeit() has no repeat argument
stmt="sum(range(100000))",
number=100,
repeat=5
)
print(result)
Sample Output: 0.08523456789012345
This means the code ran 100 times per measurement, repeated 5 times, and 0.08523 seconds is the best (fastest) of those runs.
2. Classic timeit Output Format
1000 loops, best of 5: 284 usec per loop
Breakdown:
1000 loops→ Code executed 1000 times in each timingbest of 5→ Ran the test 5 times and picked the fastest284 usec per loop→ Average time for one execution in the best run
3. Modern Benchmark Helper (2026)
import timeit
def benchmark(stmt, setup="", number=1000, repeat=7):
timer = timeit.Timer(stmt=stmt, setup=setup)
times = timer.repeat(repeat=repeat, number=number)
best = min(times) / number
print(f"{stmt[:60]:60} → {best*1_000_000:8.2f} µs per loop")
return best
# Usage
benchmark("sum(range(100000))")
benchmark("np.sum(np.arange(100000)**2)", setup="import numpy as np")
4. Best Practices for Interpreting timeit Output
- Focus on the best time (lowest value)
- Use the same
numberwhen comparing versions - Pay attention to microseconds (µs) for small functions
- Measure relative improvement (e.g., 3.2x faster)
- Use realistic data sizes
Reading timeit Results
timeit.timeit returns one float: the total seconds for number executions. timeit.repeat returns a list of such totals. The command-line interface and the IPython magic print a friendlier summary that already divides by the number of loops.
The minimum is the best estimate of the code's intrinsic cost, because higher values are mostly caused by interference from other processes. The spread between runs tells you how noisy the measurement is.
Always convert to a per-call time with a sensible unit before comparing results or writing them down.
Worked Example: From Raw Totals to a Summary
We collect raw totals and turn them into per-call statistics.
import statistics
import timeit
number = 1_000
totals = timeit.repeat("sorted(data)", setup="import random; data = random.sample(range(10_000), 1_000)",
number=number, repeat=7)
per_call = [t / number for t in totals]
print(len(totals), "measurements")
print(f"best {min(per_call) * 1e6:.1f} µs")
print(f"median {statistics.median(per_call) * 1e6:.1f} µs")
print(f"spread {(max(per_call) - min(per_call)) * 1e6:.1f} µs")
Seven raw totals become per-call times in microseconds. Reporting the best value along with the spread gives a fair, readable summary that is easy to compare with other implementations.
Practical Tips
Always note the input size next to each result, because timings only make sense for a known amount of data.
The command-line tool prints a line such as 5000 loops, best of 5: 52.3 usec per loop. Read it as: each measurement ran the statement 5000 times, five measurements were taken, and the best one corresponds to 52.3 microseconds per execution.
If the slowest result is much larger than the best, something else on the machine was busy during the test. Run the benchmark again before drawing conclusions, and keep the input size the same between runs so that the numbers can be compared.
Common Mistakes
- Treating the total of number runs as a per-call time.
- Reporting the mean, which is inflated by interference.
- Ignoring a large spread that signals a noisy machine.
- Mixing units such as ms and µs when comparing.
Quick Summary
- timeit returns totals in seconds.
- Divide by number for per-call times.
- Report the best and the spread.
- Use consistent units in comparisons.
Conclusion — Understanding timeit Output in 2026
Mastering how to read timeit output is a fundamental skill for writing efficient Python code. In 2026, developers who correctly interpret these numbers make smarter optimization decisions and build significantly faster applications.
Next steps:
- Use the
benchmark()helper in your projects