Quick Performance Checks in Jupyter: Using %timeit for Rapid Benchmarks
Use Jupyter’s %timeit magic for fast, reliable benchmarks inside notebooks. Learn how it works, see a concrete example, understand its limits, and get practical tips for accurate performance checks.
04 Jul 2025, 09:19 UTC

Why Quick Timing Matters
During exploratory data analysis you often write a handful of lines, tweak a loop, and want to know if the change actually speeds things up. Launching a full profiler or writing a separate script feels heavy‑handed. Jupyter’s built‑in %timeit magic fills that niche: it gives you a statistically meaningful measurement in a single line, without leaving the notebook.
How %timeit Works
The %timeit command is a wrapper around Python’s timeit module. When you run it, Jupyter:
- Executes the supplied code repeatedly (default 7–10 times).
- Runs a short warm‑up phase to let the interpreter “prime” the code.
- Computes the best‑time per loop across the runs and reports the number of loops and the best‑time value.
Because it runs the snippet many times, it smooths out transient CPU spikes and gives you a more reliable figure than a single measurement. The magic also accepts options: -n to set the number of loops, -r to set the number of repetitions, and -t to use the CPU’s wall clock instead of the default CPU time.
Practical Example
Open a fresh Jupyter Notebook and try the following single‑line magic:
%timeit [i**2 for i in range(1000)]
You should see output similar to:
10000 loops, best of 5: 12.3 µs per loop
The exact numbers will vary with your machine, but the format will be the same. Now do the same logic using a multi‑line cell magic:
%%timeit
result = []
for i in range(1000):
result.append(i**2)
The output will match the single‑line version, confirming that %%timeit behaves identically but lets you write longer snippets. This consistency is handy when you prototype a function and later want to benchmark it in a cell.
When %timeit Falls Short
- Stateful Code: If your snippet mutates global state or relies on objects that persist across runs,
%timeitwill re‑execute the code each time, potentially altering the result. Use-n 1 -r 1if you need a single, non‑repeated run. - Side‑Effects: Code that writes files, sends network requests, or prints to the console should not be measured with
%timeitunless you explicitly guard the side‑effects. - Precision Needs: For micro‑benchmarks that require nanosecond resolution or for measuring functions that call C extensions, the built‑in overhead of
timeitcan dominate. In those cases, switch totimeit.timeit()in a script or usecProfile/line_profiler.
Actionable Takeaways
- Use
%timeitfor ad‑hoc, informal performance checks while writing notebooks. - Run the benchmark in a clean kernel or after restarting the kernel to avoid interference from background processes.
- When you need reproducible, rigorous numbers, move the snippet to a Python script and use the
timeitmodule directly, or employ a dedicated profiler. - Remember that
%timeitreports the best of several runs; if you see wildly fluctuating numbers, try increasing the repetition count with-ror disabling the warm‑up with-n.
By keeping %timeit in your notebook toolkit, you get instant, statistically‑grounded feedback on small code blocks without the overhead of a full profiling session.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.