Verified against Claude Code · 2026-07-30
Find out why Python code is actually slow before optimizing it
A profiling-first prompt that requires naming the real bottleneck category with a specific tool and command before suggesting any optimization, instead of guessing at generic speedups.
The prompt
Ready to copy — highlighted parts are example details you can swap.
This code is slow. Before you suggest any optimization, help me find out why — an optimization based on a guess is as likely to make no difference as to help. CODE A function that dedupes a 500k-row list by checking membership against a growing Python list. SYMPTOM Takes 40+ seconds on 500k rows; was fine at 10k rows a few months ago. CONSTRAINTS ON THE FIX Must stay single-threaded (runs inside a Celery worker with its own concurrency model already). PROCESS 1. Classify the likely bottleneck category: CPU-bound (the computation itself is slow), I/O-bound (waiting on network, disk, or a database), memory-bound (excessive allocation, GC pressure, or swapping), or algorithmic (the wrong Big-O for the input size). State which you suspect and the specific line or pattern that makes you suspect it. 2. Name the exact profiling step to confirm it before changing anything: cProfile or snakeviz for CPU hotspots, py-spy for profiling a running process without modifying code, memory_profiler or tracemalloc for memory, line_profiler for line-by-line cost inside one function. Give the actual command to run, not just the tool's name. 3. Once the bottleneck is confirmed, propose the fix ranked by likely impact per effort — an algorithmic fix that changes O(n squared) to O(n) ranks above micro-optimizing a loop's variable access. 4. For any fix that trades memory for speed, or changes behavior under concurrency, say so explicitly. 5. State how to verify the fix actually helped: re-run the same profiling step and compare numbers, not "this should be faster." OUTPUT FORMAT Bottleneck hypothesis, then the profiling command to confirm it, then a ranked fix list, then the verification step.
Customize the highlighted detailsoptional — the prompt above already works
Why this works
Forcing a bottleneck classification before any fix counters the default behavior of proposing generic micro-optimizations — swapping a for loop for a list comprehension, say — that often don't touch the actual bottleneck at all and can leave the real problem completely unaddressed. Naming specific real tools with their actual invocation (cProfile's command-line module flag, py-spy for a running process with zero code changes, line_profiler for per-line cost) turns "profile your code" from an aspiration into an executable next step. Ranking fixes by algorithmic impact before micro-optimization reflects the real order-of-magnitude difference at scale: an O(n squared) membership check against a growing list, fixed by switching to a set, changes a 40-second run into a sub-second one, which no amount of loop micro-tuning would ever reach — and naming that ranking explicitly stops the model from leading with the least impactful fix just because it's the easiest to describe.
What you get back
Bottleneck hypothesis: algorithmic — "if item not in seen_list" inside the loop is an O(n) scan against a growing Python list, making the whole dedupe O(n squared). Confirm with: python -m cProfile -s cumulative dedupe_script.py — expect the membership-check line to dominate cumulative time at this input size. Fix, ranked: 1) swap seen_list for a set() — O(1) average membership check, changes the loop to O(n) overall. 2) (lower impact) avoid re-hashing unhashable row objects by deduping on a derived key instead. Verify: re-run the same cProfile command on the 500k-row input and confirm total runtime drops from ~40s to sub-second, not just "looks faster."
Verified against
Claude Code Sonnet 4.6 · 2026-07-30
Claude Sonnet 4.6 · 2026-07-31
Changelog
- 2026-07-31 — Initial publish, verified against Claude Code and Claude (Sonnet 4.6) on Python 3.12 with cProfile and py-spy.
Need this built into your business?
If a prompt isn't enough — custom software, built and maintained for you — that's Scult's day job.
EXPLORE CUSTOM SOFTWARE

