Python Performance Review Prompt
Review a Python snippet for performance opportunities while preserving behavior and separating estimates from measurements.
What this prompt does
Hotspot reasoning
Connect each proposed optimization to a specific path or measurement.
Behavior preservation
Keep output and side-effect contracts explicit.
Reproducible checks
Define equivalence cases and benchmark conditions.
What to provide & what you get
- Input
- Python code, representative inputs, expected behavior and any profiling results.
- Expected output
- A prioritized review, proposed code and a reproducible equivalence/benchmark plan.
How to use
- Supply code, representative inputs and any profile data.
- Review the proposed behavior-preserving changes.
- Run equivalence cases and benchmarks before accepting a speed claim.
Check the result: The revised code should preserve expected behavior, and any timing claim needs recorded measurements.
Copy. Paste. Run.
v1.0.0-gas1Edit the inputs below, then copy into your AI tool with the requested source material.
Before you start
- Python code, representative inputs, expected behavior and any profiling results.
Worked example · Editorial fixture
Remove repeated list scans under an explicit string-only contract
Original fictional inputs and a prepared editorial answer. This reusable prompt was not run against the fixture. Use the files and case-specific checks to review your own result.
Example input
Python 3.13.2. Function before.py keeps the first occurrence of each item by scanning the output list. Contract: a finite list of strings; preserve order, spelling and input contents. No timing data supplied.
Prepared output
CHANGE: keep the output list for order and use a set to track seen strings (after.py). OBSERVED LOCAL CHECK: six equivalence cases passed, including empty input, duplicates, original order, accents and an empty string. Inputs remained unchanged. REASONING: the original may scan a growing list for each item. Set membership avoids that repeated list scan for ordinary hashable strings; this is a complexity argument, not a measured speedup. BOUNDARY: do not substitute this version for a function accepting unhashable lists or dictionaries. BENCHMARK PLAN (not run): use timeit with fixed string datasets at 10, 1000 and 100000 items, both mostly-unique and duplicate-heavy; repeat in the same environment and check output equality first. No speed ratio is filled in.
What to check in this case
- Six actual Python equivalence checks are retained with both versions. The string-only precondition is required for the set-based change.
- No timing or universal speed claim appears; the timeit procedure remains a proposed measurement.
References: Python: timeit
Keep in mind
- Static analysis does not establish a speedup.
- A benchmark on one input shape may not represent production workloads.



