Python performance
Skill Amey-Thakur/AI-SKILLS/skills/python/python-performance
Plug-and-play skills and prompts for every AI coding agent
npx -y skills add Amey-Thakur/AI-SKILLS --skill python-performanceAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- 21 days oldThe repository was created 21 days ago. New is not bad, but a brand new repository carrying a familiar-sounding name is the shape a typosquat arrives in, and there has been no time for anyone else to find a problem with it.
- 4 stars4 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
What its author says it does
Copied from the file, not written here
Profile first, then remove Python-level overhead through vectorization, better algorithms, or a compiled extension. Use when Python code is too slow and you must decide where and how to optimize.
SKILL.md
2.3 KB, 536 tokens by cl100k_base, as published. Nobody here has run it
Python performance
Python overhead is per-operation, so wins come from doing fewer Python-level operations: better algorithms first, then batch work into C-backed calls, then compile the hot loop.
Method
- Profile before touching code.
python -m cProfile -s cumtime app.pyfor call-level,py-spy top --pid Nfor live processes without restarting,line_profilerfor a single hot function. Optimizing an unprofiled guess wastes days; the hot spot is rarely where you think. - Fix the algorithm before the language. An O(n^2) membership scan beaten into a set lookup outruns any rewrite. Check data-structure fit: list for order, set/dict for membership, deque for both-ends, heapq for top-k.
- Vectorize numeric loops. A numpy expression over an array replaces a
Python loop with one C loop, commonly 10-100x. The trap is accidental
element-wise Python (calling
float()per item, object-dtype arrays); keep dtypes numeric and operations whole-array. - Cut allocation and attribute churn in hot loops. Hoist
self.methodand global lookups to locals, reuse buffers, prefer comprehensions overappendloops. These 10-30% wins are free but only matter inside the measured hot path. - Escalate to compiled code deliberately. Order of preference: an
existing C-backed library (numpy, polars, re2),
functools.lru_cacheover recomputation, Cython or a small Rust extension (pyo3/maturin) for the one hot function. Keep the compiled surface minimal; it is the code you can no longer read in a debugger. - Verify with a benchmark, not a feeling.
timeitorpytest-benchmarkwith realistic data sizes, before and after, committed next to the code so regressions are visible.
Boundaries
- Do not optimize I/O-bound code with these techniques; overlap the waiting instead (async, threads, batching requests).
- Free-threaded and JIT builds change constants, not complexity; algorithmic wins carry across interpreters, micro-tricks may not.
- Readability is a cost you pay forever; take the 2x that keeps the code plain over the 2.3x that obfuscates it.
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.