← IndexEntry № 100·coding

Optimize a Slow Function With Specific Tradeoffs

Get a ranked list of optimizations with complexity analysis and explicit tradeoffs.

Optimized for
ClaudeChatGPT
§ When to use this

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.

§ The Prompt— fill in the fields, then copy or open in a tool
§ Customize0/3 fields filled
your prompt — fill the fields above
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].
Open with your prompt →ChatGPTClaudeSends your filled-in prompt straight into a new chat.
§ Example Output

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.

§ Pro Tips

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.
§ Variations

Adapt it for your case

Database-bound version

If the slowness is in queries, ask it to evaluate indexing, query rewrites, and N+1 elimination instead of in-memory algorithm swaps.

Memory-constrained

Set the constraint to memory and ask it to favor streaming or in-place approaches even at some latency cost.

Keep readability

Set the constraint to readability and ask for the simplest meaningful speedup a junior dev could maintain.

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

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.

§ Related Entries

You may also need