Why this prompt works
This code optimization prompt asks for the bottleneck and its complexity first, so you understand why the code is slow before changing it. Most slow code comes from a poor data structure or a loop inside a loop, and naming the exact line makes that obvious.
"Exactly the same results, including order" protects you from a faster version that quietly changes behaviour. The benchmark script lets you measure the gain on realistic data instead of trusting a claim, and preferring simple fixes before caching or new libraries keeps the code easy to maintain.
How to use this prompt
- Tap "Copy prompt".
- Replace every part in [square brackets] with your own details.
- Paste it into ChatGPT or Claude and send.
- Ask a follow-up like "make it shorter" or "give me 3 more options" to fine-tune it.
Variations to try
Slow web page or API endpoint
- Type
- Text
- Fill in
- 4 blanks
- Copies
- Be the first
Reduce memory use
- Type
- Text
- Fill in
- 3 blanks
- Copies
- Be the first
Read a profiler report
- Type
- Text
- Fill in
- 2 blanks
- Copies
- Be the first
Make it your own
- Add "must run on a small server with 512 MB of RAM" to get memory-friendly answers.
- Ask for a version using vectorised code, for example pandas or NumPy, for data work.
- Add your database type when the slow part is a query, not the code.
Common problems and fixes
- The faster version gives different results. Run both versions on the same data and compare outputs before switching.
- You optimise code that is not the real problem. Profile first and paste the output, so the AI focuses on where the time actually goes.
- The optimised code is hard to read. Ask for the simplest change that meets your target speed, with comments on why it is faster.
FAQ
Should I always optimise my code?
No. Optimise when something is measurably too slow for users or costs. Clear code is usually worth more than tiny speed gains.
What is time complexity?
It describes how running time grows as input grows. O(n) grows in step with the input; O(n squared) grows much faster and slows down quickly on large data.