← IndexEntry № 027·coding

Audit Code for Performance Bottlenecks

Identify performance bottlenecks in code and get ranked, impact-focused optimization suggestions.

Optimized for
ClaudeChatGPT
§ When to use this

This prompt turns the model into a focused performance reviewer that hunts for the specific, high-cost problems that actually slow software down: bad algorithmic complexity, repeated work, and database or I/O patterns like N+1 queries. The four numbered checks and the 'prioritize by impact' instruction force a ranked, actionable answer instead of a scattershot list, and the explicit ban on premature micro-optimizations keeps it from wasting your time on irrelevant nitpicks. Reach for it when a function or endpoint feels slow and you want a second pair of eyes before you profile.

§ The Prompt— fill in the fields, then copy or open in a tool
§ Customize0/1 fields filled
your prompt — fill the fields above
You are a performance engineering expert. Analyze the code below for performance issues. (1) Identify any O(n²) or worse algorithmic complexity. (2) Spot unnecessary re-computations or repeated work. (3) Flag N+1 query patterns or excessive I/O. (4) Suggest specific optimizations with the expected improvement. Prioritize by impact. Do not suggest premature micro-optimizations.

CODE:
[PASTE CODE]
Open with your prompt →ChatGPTClaudeSends your filled-in prompt straight into a new chat.
§ Example Output

What you can expect back

Ranked by impact:

1. N+1 query pattern (high). You run one User query and one Order query per id, so 100 ids means ~200 round trips. Fix: fetch all users in one query with User.id.in_(user_ids), then load orders with a single grouped query or a JOIN. Expected: ~200 queries down to 2, often a 10-50x latency win.

2. Counting orders by loading them (medium). len(orders) pulls every Order row just to count. Use a COUNT aggregate (func.count grouped by user_id) so you transfer counts, not full rows.

3. No handling for missing users (correctness, low perf). .first() can return None and crash on user.is_active; filter is_active in the query instead.

Not worth changing now: the list comprehension vs append micro-difference is irrelevant at this scale.

Illustrative example — your results will vary by tool and inputs.

§ Pro Tips

Get sharper results

  • 01Tell the model the realistic input size (e.g. 'user_ids is typically 5,000 long') so it can judge whether an O(n squared) section actually matters or is harmless.
  • 02Include the data access layer or ORM in the snippet, because N+1 patterns are invisible if you paste only the business logic and hide the queries.
  • 03Ask it to flag the single biggest win first and let you decide before listing the rest, which keeps you from over-optimizing a hot path that's already fast enough.
  • 04Pair this with real profiler output if you have it; paste the slow span and ask the model to reconcile its static guesses with the measured numbers.
  • 05Request a quick big-O summary table at the top so you can see the complexity of each function at a glance before reading the prose.
§ Variations

Adapt it for your case

Frontend render audit

Add 'focus on React re-renders, memoization, and bundle/network waterfalls' and paste a component instead of backend code.

SQL query plan focus

Replace the code with a slow query plus its EXPLAIN output and ask for index and join-order recommendations specifically.

Memory rather than CPU

Swap the four checks for 'find allocations in hot loops, unbounded growth, and leaks' when the symptom is memory pressure, not latency.

Best For — Roles
Use For — Tasks
Tags#performance#optimization#engineering
§ FAQ

Common questions

Can the AI actually measure the speedup it predicts?

No; its 'expected improvement' figures are educated estimates from the code structure, so treat them as a priority signal and confirm real gains with a benchmark or profiler.

How much code should I paste at once?

Paste the suspect function plus the helpers and queries it calls; too little hides bottlenecks like N+1, while pasting an entire file dilutes the analysis.

Will it suggest rewrites I can just drop in?

Often yes for clear wins, but verify behavior with tests, since an optimization that changes query semantics or ordering can subtly alter results.

§ Related Entries

You may also need