Using timeit in line magic mode is the quickest and most convenient way to get accurate, low-noise performance measurements of single statements or small expressions directly in Jupyter/IPython notebooks. The %timeit line magic runs your code many times (auto-tuned or user-specified), disables garbage collection by default, and reports mean execution time ± standard deviation, along with runs and loops — giving reliable numbers even in noisy environments (system load, caching, JIT warmup). In 2026, line magic mode remains essential for micro-benchmarks, quick comparisons, optimization experiments, and validating small changes before scaling up to full profiling or production monitoring.
Here’s a complete, practical guide to using %timeit in line magic mode: basic usage, controlling loops and runs, interpreting output, real-world patterns, and modern best practices for accurate, actionable timing in notebooks.
The simplest usage is to prepend %timeit to any single-line statement or expression — timeit auto-detects the best number of loops/runs and gives a concise summary.
%timeit x = 1 + 2
# 12.3 ns ± 0.45 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
Control precision with -n (number of loops per run) and -r (number of independent repeats) — higher values reduce noise but increase runtime; timeit auto-tunes when omitted.
# Explicit control: 100 loops per run, repeat 5 times — good for medium snippets
%timeit -n 100 -r 5 sum(range(1000))
# 1.42 µs ± 12.3 ns per loop (mean ± std. dev. of 5 runs, 100 loops each)
# Very fast snippet: many loops, fewer runs
%timeit -n 1000000 -r 3 x = 1 + 2
# 12.3 ns ± 0.45 ns per loop (mean ± std. dev. of 3 runs, 1,000,000 loops each)
# Slow code: fewer loops, more repeats for stability
%timeit -n 10 -r 20 pd.read_csv("small.csv")
Real-world pattern: comparing small operations or implementations — line magic mode is perfect for quick experiments before full profiling.
# Compare sum vs manual loop
data = list(range(1000))
%timeit sum(data) # ~1.23 µs ± 20 ns
%timeit -n 10000 sum(x for x in data) # Generator expression — slightly slower
%timeit -n 1000 [x for x in data] # List comprehension — much slower due to list creation
Best practices make line magic timing accurate and reproducible. Use high -n (10k–1M) for fast snippets (<1µs) and moderate -r (5–10) — captures noise and warmup. For slower code (>1ms), lower -n (10–1000) and higher -r (7–20) — balances precision vs. runtime. Always time the hot path — use setup code with %%timeit setup='...' for one-time init (data creation, imports). Modern tip: use -q (quiet) for clean output or -o to store results in a variable — res = %timeit -o sum(range(1000)) — then access res.average, res.stdev. Visualize — plot timings vs input size with matplotlib/seaborn for scaling behavior. In production notebooks, log timeit results — track over commits or data sizes. Combine with profilers — timeit shows total time; line_profiler or cProfile show where time is spent. Prefer generator expressions over lists — %timeit sum(x**2 for x in range(n)) avoids list creation. Avoid timing in debug mode — use release settings, disable assertions, and warm up JIT if using PyPy/Numba.
Line magic %timeit is your quick, reliable way to measure single statements in notebooks — mean gives speed, std dev shows stability, runs/loops give confidence. In 2026, control -n and -r, time the hot path, profile bottlenecks, and log metrics. Master line magic timing, and you’ll get trustworthy numbers fast — the foundation of fast, scalable Python code in interactive environments.
Next time you want to know how fast a line really is — prepend %timeit. It’s Python’s cleanest way to ask: “Is this performant?” — right in your notebook.