Join WhatsApp
Sample output Ā· AI-generated

1. Bottleneck
Lines 5 and 6 check e in seen and e not in dupes. Both are lists, so each check scans the list. With n emails this is O(n squared): about 10 billion comparisons for 100,000 emails.

2. Faster version
```python
def find_duplicates(emails):
seen = set()
dupes = {}
for e in emails:
if e in seen:
dupes[e] = None
else:
seen.add(e)
return list(dupes)
```
A set gives constant-time lookups. A dict keeps duplicates in the order they were first repeated, just like the original.

3. Complexity
O(n) time. Memory grows by one set entry per unique email, which is fine for this input size.

4. Benchmark
```python
import random, timeit
data = [f"user{random.randint(1, 50000)}@example.com" for _ in range(100000)]
print(timeit.timeit(lambda: find_duplicates(data), number=3))
```
Run it with both versions and compare the times.

5. Next steps
If emails differ only by case, normalise them once with lower() before the loop.

Text prompt Ā· ChatGPT Ā· Claude Ā· Gemini

Code optimization prompt to find and fix slow code

Find out why a piece of code is slow, get a faster version that gives the same results, and learn how to measure the difference.

You are a performance engineer for [language]. Help me make this code faster.

Code:
```
[paste code]
```
Typical input size: [e.g. 200,000 rows, 5 MB file]
Current speed and target: [e.g. 40 seconds, need under 2]
Profiler output, if any: [paste]
Constraints: [memory limit, cannot add libraries, must keep output order]

Answer in this order:
1. The likely bottleneck and its time complexity, with the line it comes from.
2. A faster version that returns exactly the same results, including order and edge cases.
3. The new time complexity and any memory trade-off.
4. A short benchmark script to compare old and new versions on realistic data.
5. Other improvements to consider if this is still not fast enough.

Prefer simple changes (better data structures, fewer loops) before caching, parallel code or new libraries.
Type
Text
Fill in
6 blanks
Copies
Be the first

Updated Ā· By

Tip: Always measure before and after on realistic data; a change that feels faster can be slower in practice.

1. CopyTap "Copy prompt" above.
2. Fill the blanksReplace each [bracket] with your own details.
3. Paste + refinePaste it into the chat, then ask for changes.

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

  1. Tap "Copy prompt".
  2. Replace every part in [square brackets] with your own details.
  3. Paste it into ChatGPT or Claude and send.
  4. 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

ChatGPT Claude Gemini
My [framework] endpoint [path] takes [time] to respond. Here is the handler and the database queries it runs: [paste]. Look for N+1 queries, missing indexes, large payloads and repeated work. Suggest fixes in order of likely impact and how to measure each.
Type
Text
Fill in
4 blanks
Copies
Be the first

Reduce memory use

ChatGPT Claude Gemini
This [language] script runs out of memory on a [size] file: [paste code]. Rewrite it to process the data in a streaming way or in chunks, keeping the same output. Explain the memory use before and after.
Type
Text
Fill in
3 blanks
Copies
Be the first

Read a profiler report

ChatGPT Claude Gemini
Here is profiler output from [tool, e.g. cProfile or Chrome DevTools]: [paste]. Explain in plain English where the time goes, which 3 functions to look at first, and what kind of change usually helps each.
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.

More Coding prompts

See all
ChatGPT Claude Text prompt

Debug code prompt that finds the root cause before fixing

Paste your code and the error message to get the real cause explained first, then a small fix and a test for it.

4 prompts
ChatGPT Claude Text prompt

Explain code prompt for beginners, line by line

Paste any snippet and get a plain-English summary, a line-by-line walkthrough and the concepts you need to understand it.

4 prompts
ChatGPT Claude Text prompt

Unit test prompt for edge cases in pytest or Jest

Get a list of test cases for a function, then ready-to-run tests for normal input, edge cases and errors in your framework.

3 prompts
ChatGPT Claude Text prompt

Refactor code prompt for cleaner, more readable functions

Clean up a messy function while keeping its behaviour the same, with a short list of every change and the reason for it.

4 prompts
ChatGPT Claude Text prompt

SQL query prompt: describe your tables, get the query

Describe your tables and the question you want answered, and get a correct query for your database with a short explanation.

4 prompts
ChatGPT Claude Text prompt

Regex prompt that writes and explains a pattern with tests

Describe what you want to match and get a working pattern for your language, explained piece by piece, with examples that should and should not match.

4 prompts
ChatGPT Claude Text prompt

Code review prompt for bugs, security and readability

A structured review of a pull request or snippet, with issues sorted by severity, each tied to a line and a suggested fix.

3 prompts
ChatGPT Claude Text prompt

API design prompt for clean REST endpoints and errors

Plan a REST API for a feature: endpoints, example requests and responses, status codes, error format and the decisions behind them.

4 prompts