python-performance-optimization

Profiles and optimizes Python code using cProfile, memory profilers, and benchmarking patterns.

Updated Feb 8, 2026
One-click install
npx skills add https://github.com/ttnhan18062000/rpg-based-simulation --skill python-performance-optimization-ttnhan18062000
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-performance-optimization
Source: https://github.com/ttnhan18062000/rpg-based-simulation/tree/main/.agents/skills/python-performance-optimization
Command: npx skills add https://github.com/ttnhan18062000/rpg-based-simulation --skill python-performance-optimization-ttnhan18062000

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Python applications often suffer from hidden CPU bottlenecks, memory leaks, and slow I/O paths that are hard to locate without systematic profiling. This Skill provides concrete profiling workflows and optimization patterns to find and fix real performance problems instead of guessing. ## Core Features & Use Cases - CPU and Line Profiling: Use cProfile, line_profiler, and py-spy to identify time-consuming functions at function and line granularity, including live production processes. - Memory Analysis: Track allocations with memory_profiler and tracemalloc, detect leaks, and reduce footprint with generators, slots, and weak references. - Optimization Patterns: Apply proven techniques such as list comprehensions, dict lookups, lru_cache, NumPy vectorization, multiprocessing, async I/O, and batch database operations. - Use Case: A simulation tick loop runs slower than its p95 latency baseline. Profile the hot path with cProfile, discover an unnecessary O(N) scan, replace it with a dict-based lookup, and verify the fix against the repo's PerfRegressionGate. ## Quick Start Profile my Python script to find the slowest functions and suggest concrete optimizations to reduce its execution time.

Frequently Asked Questions about python-performance-optimization

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

FAQPage Schema
How do I profile Python code to find performance bottlenecks?

Use cProfile to profile Python code by wrapping execution with profiler.enable() and profiler.disable(), then inspect results with pstats sorted by cumulative time. For line-by-line detail, use line_profiler with kernprof, and for running production processes use py-spy.

What is the difference between cProfile and py-spy for Python profiling?

cProfile is a deterministic profiler that instruments code and adds overhead, making it suited for development. py-spy is a sampling profiler that attaches to running processes with low overhead, making it suitable for profiling production systems and generating flamegraphs.

How do I detect memory leaks in Python applications?

Use tracemalloc to take memory snapshots before and after running code, then compare them to find the lines allocating the most memory. The memory_profiler package with its @profile decorator also shows per-line memory consumption when run via python -m memory_profiler.

When should I use multiprocessing versus asyncio in Python?

Use multiprocessing for CPU-bound tasks like heavy calculations, since it bypasses the GIL by running across multiple processes. Use asyncio with libraries like aiohttp for I/O-bound work such as concurrent HTTP requests, where tasks spend time waiting rather than computing.

Why is my Python code slow even after optimization?

Common causes include optimizing without profiling first, using lists where dict lookups are needed, excessive function call overhead in hot loops, and unnecessary data copies. Always measure with timeit or cProfile to confirm the actual bottleneck before changing code.