- Home
- Skills
- Code Quality & Review
- Performance Optimization: Profile First, Then Fix
Works with the AI Agents you already use
Performance Optimization: Profile First, Then Fix
Profiles before touching code, fixes the real bottleneck, and proves the speedup with the same measurement.
Free
Performance Optimization: Profile First, Then Fix
Example session with this skill installed
Use the performance-optimization skill on report.py. It takes about 8 seconds on 12,000 signups and the real nightly batch is 500,000. Make it fast enough for that.
(report.py source: https://github.com/hemapriyan-rk/claude-rigor-skills/blob/main/examples/performance-optimization/report_before.py)
- Read your context and instructions
- Compiled the performance optimization
Result
Runtime: about 8 s → 0.15 s (~55×) on 12,000 signups, which includes interpreter startup. The 500,000-signup nightly size now takes 0.52 s for the report steps (unique_emails, count_by_country, to_csv), excluding the time to generate the fake input. The old code would have taken roughly 4 hours at that size, assuming the O(n²) scaling holds.
Bottleneck
cProfile on the 12k run showed 97% of the time (49.0 of 50.5 s, inflated by profiler overhead) in count_by_country. For every unique email it rescanned the whole signup list. unique_emails was another 1.2 s, because email not in seen searched a list. Both were O(n²) and CPU-bound.
Change
In report.py:
unique_emails:list(dict.fromkeys(...)). This is O(n) and keeps first-seen order.
count_by_country: one pass builds a map of email to the country of its first signup, then each unique email is tallied from that map. This matches the old "first match wins" behavior. An email missing from signups is still skipped, as before.
I did nothing to to_csv, which isn't on the hot path. A dict lookup was enough, so I didn't try anything more elaborate.
Verified
- I re-ran
python report.pyon the same 12,000 signups and compared it tobaseline.txt. There are 0 differences. - The 500k timing is a single run. At that size, 489,616 of the emails are unique.
- Only the 12k output was compared to the baseline. I didn't run the old code at 500k, so I haven't confirmed the two versions agree there.
I also left an after.txt in the directory from the comparison. You can delete it.
The changed functions in report.py, as written by the agent:
def unique_emails(signups):
return list(dict.fromkeys(s["email"] for s in signups))
def count_by_country(signups, emails):
first_country = {}
for s in signups:
first_country.setdefault(s["email"], s["country"])
counts = {}
for email in emails:
country = first_country.get(email)
if country is not None:
counts[country] = counts.get(country, 0) + 1
return counts
Independently re-checked: 7.5 s → 0.2 s on the same 12,000 rows, byte-identical output.
Connects securely to your tools. The creator never sees your data.
About this skill
What it does
Makes slow code or systems faster by measuring first. It profiles the real hot path, classifies the bottleneck (CPU, I/O, or memory), fixes the highest-leverage cause, and re-runs the same measurement to prove the speedup, with output checked to be unchanged.
Why it exists
"It's probably the loop" is a guess, and guesses cost a day when they're wrong. AI assistants tend to micro-optimize whatever looks inefficient. This skill makes measurement the first step and stops when the target is met.
How it works
- Agrees on the target: which metric, on what input, and what number counts as done.
- Reproduces the slowness and profiles it, using a bundled table of profilers for Python, Node.js, Go, Java, Rust, C/C++, .NET, SQL, and browsers.
- Classifies the bottleneck before choosing a fix, so it doesn't speed up a loop when the time is spent waiting on I/O.
- Fixes in order of leverage: algorithm or query shape, then data structure, then I/O pattern, then micro-optimization.
- Re-runs the exact baseline and confirms the output is unchanged. A faster wrong answer is a regression.
What you get
Before/after numbers on the agreed metric, the profiler evidence for the bottleneck, the change with the reason it was picked, and how the result was verified.
When not to use it
Capacity planning for infrastructure you can't run, or tuning without any way to execute the code. Without shell access it gives you the exact profiling commands instead.
Open source
Part of claude-rigor-skills: 30 MIT-licensed skills that make AI show its work. The demo below, with its inputs, is in the examples folder.
- Needs to run your code for real measurements. Without shell access it falls back to giving you profiling commands to run.
- Measurements reflect the machine it runs on; production hardware and load can differ.
- Focuses on single-program and query performance. System-wide capacity and cost tuning is better served by an architecture skill.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before any payment.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
10 uses
Times used to date
Free forever
No account required to browse
Trust & safety
Security scan
Security scan passed 1 month ago
- Free with an Agensi account
Permissions
Runs profilers and your own program to measure it, and edits only the code being optimized. It does not install packages or use the network.