Topic 5 of 5 · 5 min read
Profiling and benchmarking before optimising
Goal: Use a profile or query plan to confirm where AI-written code is slow and check that a proposed fix actually helps.
Measure first, change one thing, and measure again; a performance fix that was not measured is only a guess. AI assistants are very willing to "optimise" code, and they will do it confidently without data. Your job is to ask for the evidence.
Check yourself · 1 of 3
What should you do before asking an agent to optimise a slow endpoint?
Why it matters
Most of the time in a slow request is spent in a small part of the code, often one query or one loop. If an agent rewrites a function that takes 2% of the time, you get a bigger diff, more risk and no speedup. Worse, some "optimisations" are slower: an extra index slows writes, a cache adds a memory leak, a clever rewrite is harder to read and no faster.
The tools
- A CPU profiler.
cProfilein Python,--profor the Chrome DevTools profiler in Node.js,pprofin Go. It shows which functions use the time and how many times they are called. - A flame graph. A picture of the profile where wide boxes are where the time goes. Brendan Gregg, who invented them, explains how to read one.
- A query plan.
EXPLAINshows how the database will run a query;EXPLAIN ANALYZEruns it and shows the real time and row counts. A sequential scan on a big table, or an estimate far from the actual count, points at a missing index or stale statistics. - A benchmark. A repeatable test of one operation, with a realistic input size, warm-up, and several runs. Compare medians, not a single lucky run.
- A query counter. Counts queries per request to expose N+1 problems.
A working routine
- Define the goal: "the report endpoint should answer in under 500 ms at 50,000 rows".
- Reproduce with realistic data.
- Profile and find the top cost.
- Ask the agent to fix only that, and to show the before and after numbers.
- Re-run the same measurement. Keep the change only if the numbers moved enough to matter, and the tests still pass.
Before and after
Weak prompt: "Make this endpoint faster."
Better: "GET /reports takes 4.1 s at 50k rows. The profile shows 3.6 s in
load_orders(). EXPLAIN ANALYZE shows a sequential scan on orders.
Propose one fix for load_orders only, and show timings before and after."
What goes wrong
- Benchmarking tiny data. A cache or index can look useless, or useful, at the wrong size.
- Measuring once. Noise from other processes changes single runs; repeat them.
- Optimising for the benchmark. A fix that wins on one input and loses on real traffic is not a fix.
Examples
The index that was not the problem
An agent suggests adding an index because the endpoint is slow. The profile shows 90% of the time in a Python loop that builds JSON, and EXPLAIN ANALYZE shows the query takes 12 ms. The index would have added write cost and fixed nothing. The fix was to stop building the response one field at a time.
A micro-benchmark with numbers
import timeit
setup = "ids = list(range(50000)); s = set(ids); target = 49999"
print(min(timeit.repeat("target in ids", setup, number=200, repeat=5)))
print(min(timeit.repeat("target in s", setup, number=200, repeat=5)))
Run it at the real input size, take several repeats, and report both numbers. The claim "the set is faster" now has evidence.
Key points
- Profile before changing anything, because most time sits in a small part of the code.
- A flame graph shows time by width, and a query plan shows how the database actually runs a query.
- Benchmark with realistic input sizes, repeat the run and compare medians rather than a single result.
- Give the agent the measurement and ask for one targeted change with before and after numbers.
- Keep an optimisation only if the numbers improve enough to matter and the tests still pass.
Common mistakes
- Accepting an optimisation because the agent described it as faster.
- Profiling on a laptop sample that is much smaller than production data.
- Changing several things at once, so nobody knows which one helped.
- Comparing one run before with one run after.
Check yourself · 2 of 3
In a flame graph, what does a wide box mean?
Check yourself · 3 of 3
An agent says its rewrite makes a function 5x faster. What is the best way to check?
Practice items, not counted towards your mastery.
Start here
- Use The Index, Luke: SQL indexing and tuning (opens in a new tab)BookFree
A free book on how indexes and query shape decide database speed.
- Using EXPLAIN (PostgreSQL documentation) (opens in a new tab)DocsFree
How to read a query plan, with estimated and actual costs.
- Flame Graphs (Brendan Gregg) (opens in a new tab)ArticleFree
The inventor explains how to read a flame graph to find where time goes.