Skip to content

Time a statement

A single call to a fast function is over too quickly to measure — one reading is mostly noise. The usual workaround is to write a throwaway script that runs the thing a few thousand times and profile that. profit timeit is that script, built in: hand it the expression and it owns the loop, the one-time setup, and the iteration count, so you skip the boilerplate (and skip deleting it afterward).

profit timeit 'sorted(data)' --setup 'data = list(range(1000, 0, -1))'

It works much like Python's built-in timeit module, but you get profit's full per-call breakdown — mean, spread, min/max — instead of a single total.

profit timeit profiling a statement over many iterations

--setup runs once before timing — use it to import modules or build inputs — and the timed statement then runs in a tight loop. The iteration count is chosen automatically so the whole run takes about 0.2 s (the count for this run is shown in the header).

Fixing the iteration count

Pass -n to run an exact number of iterations instead of the automatic count:

profit timeit 'sum(range(10_000))' -n 5000

Comparing two statements

timeit takes the same -b/-p flags as profit run. Import both functions in --setup and pit them against each other:

profit timeit 'g()' --setup 'from mymod import f, g' -b f -p g

Next