Using timeit in cell magic mode (%%timeit) is the most convenient way in Jupyter notebooks to accurately time larger blocks of code, multi-line statements, or entire functions — everything inside the cell runs as a single unit many times, giving reliable average execution time, standard deviation, and loop/run counts. Unlike line magic (%timeit) for single expressions, cell magic handles setup, loops, conditionals, and function definitions — perfect for benchmarking real code paths, comparing implementations, or measuring I/O-heavy or complex logic. In 2026, cell magic remains essential for interactive optimization, algorithm tuning, regression testing in notebooks, and validating performance before scaling to scripts or production.
Here’s a complete, practical guide to using %%timeit in cell magic mode: basic usage, controlling loops and runs, interpreting output, real-world patterns, and modern best practices for accurate, actionable timing in notebooks.
Place %%timeit at the very top of a cell — the entire cell body (including imports, variables, loops, or functions) is timed as a unit. Timeit auto-tunes the number of loops/runs for stability and speed.
%%timeit
total = 0
for i in range(1000):
total += i ** 2
# 145 µs ± 2.34 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Control precision with -n (loops per run) and -r (independent repeats) — higher values reduce noise but increase runtime; timeit auto-tunes when omitted.
%%timeit -n 100 -r 10
total = 0
for i in range(1000):
total += i ** 2
# 142 µs ± 1.12 µs per loop (mean ± std. dev. of 10 runs, 100 loops each)
%%timeit -n 100000 -r 3
x = 1 + 2
# Very fast snippet: many loops, fewer repeats
Real-world pattern: comparing implementations of the same logic — cell magic mode is perfect for timing multi-line functions or blocks.
%%timeit -n 1000 -r 5
# Manual loop version
data = list(range(10000))
total = 0
for x in data:
total += x
%%timeit -n 1000 -r 5
# Built-in sum version
data = list(range(10000))
total = sum(data)
# Compare: sum() is usually 5–10× faster — cell magic gives stable numbers
Best practices make cell magic timing accurate, reproducible, and actionable. Place %%timeit at the cell’s very top — no code above it (timeit executes the cell as-is). Use high -n (10k–1M) for fast blocks (<1ms) and moderate -r (5–10) — captures noise and warmup. For slower code (>1ms), lower -n (100–1000) and higher -r (7–20) — balances precision vs. runtime. Use setup code with %%timeit setup='...' for one-time init — e.g., data creation or imports — so you don’t time setup repeatedly. Modern tip: use -o to store results in a variable — res = %%timeit -o ... — then access res.average, res.stdev, res.best. 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 generators over lists for large data — 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.
Cell magic %%timeit is your quick, reliable way to measure multi-line code or functions 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 cell 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 block or function really is — put %%timeit at the top of the cell. It’s Python’s cleanest way to ask: “Is this performant?” — right in your notebook.