python-performance

Profiles and optimizes Python code using cProfile, PyInstrument, memray, and pytest-benchmark.

1|2|Updated Nov 25, 2017
One-click install
npx skills add https://github.com/asarchami/dotfiles --skill python-performance-asarchami
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-performance
Source: https://github.com/asarchami/dotfiles/tree/main/dot_config/opencode/skills/python/performance
Command: npx skills add https://github.com/asarchami/dotfiles --skill python-performance-asarchami

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires pyinstrument, memray, pytest-benchmark.

What problem does it solve? Python applications often suffer from hidden performance bottlenecks and memory leaks that are hard to locate without proper tooling. This Skill provides structured guidance for profiling CPU usage, analyzing memory allocation, benchmarking code, and applying proven optimization patterns. ## Core Features & Use Cases - CPU Profiling: Run scripts with PyInstrument for readable statistical output or cProfile for detailed cumulative timing. - Memory Analysis: Use memray and tracemalloc to detect memory leaks and inspect allocation hotspots line by line. - Benchmarking: Write regression-safe benchmarks with pytest-benchmark and compare results across runs. - Use Case: A data pipeline runs slower after a refactor. Profile it with PyInstrument to find the bottleneck function, apply an algorithmic fix such as replacing list membership checks with a set, then add a pytest-benchmark test to prevent regression. ## Quick Start Ask the AI to profile your slow Python script with PyInstrument and suggest optimizations for the top bottleneck functions.

Frequently Asked Questions about python-performance

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I profile a Python script to find bottlenecks?

Run python -m pyinstrument script.py for readable statistical output, or python -m cProfile -s cumulative script.py for detailed built-in timing. PyInstrument is preferred for quick identification of slow functions.

PyInstrument vs cProfile: which profiler should I use?

PyInstrument is a statistical profiler with lower overhead and more readable output, ideal for quick analysis. cProfile is deterministic and built into Python, providing detailed call counts and cumulative timing for deeper investigation.

How do I find memory leaks in Python code?

Install memray and run memray run script.py, then generate a flamegraph with memray flamegraph to visualize allocations. Alternatively, use the built-in tracemalloc module to snapshot and compare memory usage by line number.

How do I benchmark Python functions with pytest?

Use pytest-benchmark by adding a benchmark fixture to your test and calling benchmark(func, *args). Run pytest with --benchmark-only to execute benchmarks, and --benchmark-compare to detect regressions across runs.

What are common Python performance optimization techniques?

Use sets for O(1) membership checks, deque for queue operations, generators for large data streams, functools.lru_cache for expensive computations, and str.join for string building. Always profile first and improve algorithms before micro-optimizations.