python-performance
Profile and optimize Python code using cProfile, memory profilers, andperformance best practices.Triggers: profiling, optimization, cProfile, memory profiler, bottleneck,slow code, performance, benchmarking, py-spy, tracemallocUse when: debugging slow code, identifying bottlenecks, optimizing memory,benchmarking performance, production profilingDO NOT use when: async concurrency - use python-async instead.DO NOT use when: CPU/GPU system monitoring - use conservation:cpu-gpu-performance.Consult this skill for Python performance profiling and optimization.
When & Why to Use This Skill
This Claude skill provides a comprehensive toolkit for profiling and optimizing Python code. It enables developers to identify execution bottlenecks, analyze memory usage, and apply performance best practices using industry-standard tools like cProfile, py-spy, and tracemalloc to ensure efficient, scalable, and high-performance applications.
Use Cases
- Identifying Performance Bottlenecks: Use cProfile and line profiling to pinpoint exactly which functions or lines of code are slowing down your application.
- Memory Leak Detection and Optimization: Analyze memory consumption patterns and track down leaks using tracemalloc to improve application stability.
- Production Profiling: Safely monitor and diagnose performance issues in live Python environments using low-overhead tools like py-spy.
- Benchmarking Code Changes: Quantify the impact of optimizations and prevent performance regressions by implementing automated benchmark suites.
- Algorithm and Data Structure Optimization: Apply proven patterns such as caching, generators, and efficient data structures to reduce CPU latency and resource usage.
| name | python-performance |
|---|---|
| description | | |
| Triggers | profiling, optimization, cProfile, memory profiler, bottleneck, |
| Use when | debugging slow code, identifying bottlenecks, optimizing memory, |
| DO NOT use when | CPU/GPU system monitoring - use conservation:cpu-gpu-performance. |
| category | performance |
| tags | [python, performance, profiling, optimization, cProfile, memory] |
| tools | [profiler-runner, memory-analyzer, benchmark-suite] |
| complexity | intermediate |
| estimated_tokens | 1200 |
| progressive_loading | true |
Python Performance Optimization
Profiling and optimization patterns for Python code.
Quick Start
# Basic timing
import timeit
time = timeit.timeit("sum(range(1000000))", number=100)
print(f"Average: {time/100:.6f}s")
Verification: Run the command with --help flag to verify availability.
When to Use
- Identifying performance bottlenecks
- Reducing application latency
- Optimizing CPU-intensive operations
- Reducing memory consumption
- Profiling production applications
- Improving database query performance
Modules
This skill is organized into focused modules for progressive loading:
profiling-tools
CPU profiling with cProfile, line profiling, memory profiling, and production profiling with py-spy. Essential for identifying where your code spends time and memory.
optimization-patterns
Ten proven optimization patterns including list comprehensions, generators, caching, string concatenation, data structures, NumPy, multiprocessing, and database operations.
memory-management
Memory optimization techniques including leak tracking with tracemalloc and weak references for caches. Depends on profiling-tools.
benchmarking-tools
Benchmarking tools including custom decorators and pytest-benchmark for verifying performance improvements.
best-practices
Best practices, common pitfalls, and exit criteria for performance optimization work. Synthesizes guidance from profiling-tools and optimization-patterns.
Exit Criteria
- Profiled code to identify bottlenecks
- Applied appropriate optimization patterns
- Verified improvements with benchmarks
- Memory usage acceptable
- No performance regressions
Troubleshooting
Common Issues
Command not found Ensure all dependencies are installed and in PATH
Permission errors Check file permissions and run with appropriate privileges
Unexpected behavior
Enable verbose logging with --verbose flag