Speed Up a Slow Function with Big-O Analysis and a Benchmark
You get the exact bottleneck line, current vs. proposed Big-O, an optimized drop-in function, and a small benchmark that proves the speedup before you adopt it.
Prompt
Act as a senior performance engineer who reviews code for speed problems.
I will paste a slow function right after this message, with its language and how it is called. If I leave out the typical input size or how the function is used, ask me up to 3 short numbered questions in one message and wait for my answers before you start.
Rules:
1. State the current and proposed time complexity in Big-O, and define every variable you use (for example, n = number of orders).
2. Quote the exact line or lines causing the bottleneck and explain in one or two sentences why they are slow.
3. Keep behavior identical: same outputs, same ordering, same edge cases. If any change alters behavior, say so plainly.
4. Prefer the standard library before adding dependencies.
5. Give a small, self-contained benchmark that runs both versions on realistic input sizes and asserts they return the same result.
Output format:
1. Bottleneck: quoted line plus explanation
2. Complexity: table with columns Version, Time, Space, Notes
3. Optimized code: full function, with short comments on changed lines
4. Benchmark: runnable snippet and how to run it
5. Expected gain: rough estimate, labeled as an estimate until I run the benchmark
End with one line on assumptions you made and any risks, such as higher memory use or input cases the benchmark does not cover.Example output
Tips and common edits
Why this works
- Requiring the exact bottleneck line forces a real diagnosis instead of generic tips like 'use a set'.
- The behavior-identical rule catches subtle breaks such as changed output order or edge cases.
- An asserting benchmark lets you verify the speedup and correctness yourself before trusting the estimate.
Use when a function is noticeably slow on real data. Paste the full function into ChatGPT, Claude or Gemini, and note the language, typical input size, and whether output order matters.
Common edits
- Add a memory limit, e.g. 'the input may not fit in RAM', to get streaming-friendly suggestions.
- Ask for two options: a quick fix and a deeper rewrite, with tradeoffs for each.
Prompt details
- By
- Prompts-GPT editors
- Published
Did this prompt work?
Keep exploring
More like this
Get Socratic Coding Hints in Three Levels Before Seeing the Full Solution
A patient tutor that gives escalating hints on your coding exercise, one level at a time, and shows full code only after you have tried twice.
Optimize a Slow SQL Query Using Its EXPLAIN Plan Step by Step
Paste a slow query and its EXPLAIN output to get the costliest plan node named, exact CREATE INDEX or rewrite fixes, and a clear way to verify the speedup.
Explain Pasted Code Line by Line with a Worked Example for Beginners
Get a block-by-block walkthrough of any code you paste, with one example input traced through every step and three questions to check that you understood it.
Explain a Confusing Coding Tutorial or Docs Passage in Plain English
Paste a confusing passage from a programming tutorial or docs and get a plain-English rewrite, inline jargon definitions, a tiny runnable example and a list of what to learn first.
Explain Any Regex Token by Token with Matching and Failing Examples
Get a token-by-token table explaining your regex, plus strings that match, nearly match and fail, and a clear catastrophic backtracking risk check with a safer rewrite.
Review a Beginner's Code with Kind, Prioritized Feedback on the Top 3 Fixes
Paste your code and get a mentor-style review with one specific compliment, the three highest-impact fixes ranked with corrected snippets, and one next step.
- Write a Safe Automation Script with Dry Run and Error Handling
- Characterization Tests Before a Refactor
- Choose Your First Programming Language Based on Your Goals, Time and Device
- Add Docstrings and Why-Focused Comments to Your Pasted Code
- Refactor Pasted Code for Readability Without Changing Its Behavior
- Port a Function or Module to Another Language With Tests Proving Same Behavior