Optimize a Slow Function With Specific Tradeoffs
Get a ranked list of optimizations with complexity analysis and explicit tradeoffs.
This prompt diagnoses why a function is slow at a given input size and returns ranked optimization options, each with its new complexity and an honest statement of what you'd trade away. Use it when you've found a hot path and need to choose an approach deliberately rather than reaching for the first clever trick. The forced tradeoff column is the real value — it stops the model from recommending a faster algorithm that quietly costs you memory or readability you can't afford.
This function is too slow at [INPUT SIZE]. Code: [PASTE CODE] Analyze: (1) current time and space complexity, (2) the bottleneck operation, (3) 2-3 optimization approaches ranked by impact, (4) for each, the new complexity and what you're trading off (memory, code clarity, edge case handling), (5) which I should pick given my constraint: [LATENCY/MEMORY/READABILITY].
What you can expect back
1. Current: O(n^2) time, O(1) extra space. The nested loop compares every pair. 2. Bottleneck: the inner loop's pairwise email comparison — ~1.25B comparisons at n=50k. 3. Approaches, ranked: a. Hash set of seen emails — O(n) time, O(n) space. Single pass, append on collision. b. Sort by email then scan adjacent — O(n log n) time, O(1)-O(n) space. No hash needed. c. Counter/group-by email — O(n), O(n) space, and gives you counts for free. 4. Tradeoffs: (a) costs O(n) memory but trivially readable; (b) mutates/copies order, slightly more code; (c) most memory, most flexible output. 5. Pick (a). In a request handler latency dominates and 50k emails of memory is negligible — the O(n) hash-set pass is the clear win.
Illustrative example — your results will vary by tool and inputs.
Get sharper results
- 01State your real input distribution, not just size — 'mostly unique' versus 'heavily duplicated' can change which approach wins.
- 02Paste the actual code including the data structures it operates on, since the best optimization often depends on whether you're holding a list, generator, or DB cursor.
- 03Name your hard constraint precisely; 'latency in a request handler' and 'memory on an embedded device' lead to opposite recommendations.
- 04Ask it to flag any approach that changes observable behavior (ordering, returning duplicates vs. uniques) so you don't trade speed for a subtle bug.
- 05Request a quick benchmark snippet for the top pick so you can confirm the speedup on your real data rather than trusting Big-O alone.
Adapt it for your case
If the slowness is in queries, ask it to evaluate indexing, query rewrites, and N+1 elimination instead of in-memory algorithm swaps.
Set the constraint to memory and ask it to favor streaming or in-place approaches even at some latency cost.
Set the constraint to readability and ask for the simplest meaningful speedup a junior dev could maintain.
Common questions
Will it actually profile my code?
It reasons about complexity from the code you paste, not a live profile. For real hotspots, run a profiler first and paste the slow function it identifies.
What if the bottleneck is I/O, not CPU?
Tell the model so — it will pivot to batching, caching, or async strategies instead of algorithmic complexity, which won't help I/O-bound code.
Can I trust the new complexity claims?
They're usually right but verify by benchmarking the recommended approach on representative input before shipping it.
You may also need
Perform a Thorough Code Review on a Pull Request
Get a senior-engineer-style code review with categorized, file-referenced feedback.
Refactor Code for Readability and Maintainability
Refactor any code for readability and maintainability without changing its behavior.
Audit Code for Performance Bottlenecks
Identify performance bottlenecks in code and get ranked, impact-focused optimization suggestions.
Run a Security Review on Code
Get an OWASP-aligned security review with severity ratings and remediation snippets.