Understanding timeit Output in Python 2026 with Efficient Code

Efficient Code Mar 15, 2026

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

  • timeit shows 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 + min for 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 timing
  • best of 5 → Ran the test 5 times and picked the fastest
  • 284 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 number when 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

Related Tutorials

Share:
Last updated: March 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices