Python Performance Optimizer
Python performance optimization

Profile and optimize Python code using cProfile, memory profilers, and performance best practices. Use when debugging slow Python code, optimizing bottlenecks, or improving application performance.
--- name: python-performance-optimization description: Profile and optimize Python code using cProfile, memory profilers, and performance best practices. Use when debugging slow Python code, optimizing bottlenecks, or improving application performance. --- # Python Performance Optimization Comprehensive guide to profiling, analyzing, and optimizing Python code for better performance, including CPU profiling, memory optimization, and implementation best practices. ## When to Use This Skill - Identifying performance bottlenecks in Python applications - Reducing application latency and response times - Optimizing CPU-intensive operations - Reducing memory consumption and memory leaks - Improving database query performance - Optimizing I/O operations - Speeding up data processing pipelines
Overview
Comprehensive guide to profiling, analyzing, and optimizing Python code for better performance, including CPU profiling, memory optimization, and implementation best practices.
Manual install (npx, ZIP)
npx skills add wshobson/agents --skill python-performance-optimization -g -a claude-codemkdir -p ~/.claude/skills && curl -sL https://codeload.github.com/wshobson/agents/tar.gz/4236bb91f839 \
| tar -xz -C ~/.claude/skills --strip-components=4 agents-4236bb91f839/plugins/python-development/skills/python-performance-optimizationNew-Item -ItemType Directory -Force "$HOME\.claude\skills" | Out-Null; Invoke-WebRequest "https://m.agentalley.io/zip/d0ad23e0f232b0210713/python-performance-optimization.zip" -OutFile "$env:TEMP\python-performance-optimization.zip"; Expand-Archive "$env:TEMP\python-performance-optimization.zip" -DestinationPath "$HOME\.claude\skills" -ForceConnects with Agent Alley MCP · agentalley.io/mcp
Files included
SKILL.md3.2 KBreferences/2 files · 17.5 KB
Read the files here
Show the full text101 lines
Python Performance Optimization
Comprehensive guide to profiling, analyzing, and optimizing Python code for better performance, including CPU profiling, memory optimization, and implementation best practices.
When to Use This Skill
- Identifying performance bottlenecks in Python applications
- Reducing application latency and response times
- Optimizing CPU-intensive operations
- Reducing memory consumption and memory leaks
- Improving database query performance
- Optimizing I/O operations
- Speeding up data processing pipelines
- Implementing high-performance algorithms
- Profiling production applications
Core Concepts
1. Profiling Types
- CPU Profiling: Identify time-consuming functions
- Memory Profiling: Track memory allocation and leaks
- Line Profiling: Profile at line-by-line granularity
- Call Graph: Visualize function call relationships
2. Performance Metrics
- Execution Time: How long operations take
- Memory Usage: Peak and average memory consumption
- CPU Utilization: Processor usage patterns
- I/O Wait: Time spent on I/O operations
3. Optimization Strategies
- Algorithmic: Better algorithms and data structures
- Implementation: More efficient code patterns
- Parallelization: Multi-threading/processing
- Caching: Avoid redundant computation
- Native Extensions: C/Rust for critical paths
Quick Start
Basic Timing
import time
def measure_time():
"""Simple timing measurement."""
start = time.time()
# Your code here
result = sum(range(1000000))
elapsed = time.time() - start
print(f"Execution time: {elapsed:.4f} seconds")
return result
# Better: use timeit for accurate measurements
import timeit
execution_time = timeit.timeit(
"sum(range(1000000))",
number=100
)
print(f"Average time: {execution_time/100:.6f} seconds")
Detailed patterns and worked examples
Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.
Best Practices
- Profile before optimizing - Measure to find real bottlenecks
- Focus on hot paths - Optimize code that runs most frequently
- Use appropriate data structures - Dict for lookups, set for membership
- Avoid premature optimization - Clarity first, then optimize
- Use built-in functions - They're implemented in C
- Cache expensive computations - Use lru_cache
- Batch I/O operations - Reduce system calls
- Use generators for large datasets
- Consider NumPy for numerical operations
- Profile production code - Use py-spy for live systems
Common Pitfalls
- Optimizing without profiling
- Using global variables unnecessarily
- Not using appropriate data structures
- Creating unnecessary copies of data
- Not using connection pooling for databases
- Ignoring algorithmic complexity
- Over-optimizing rare code paths
- Not considering memory usage
| 1 | |
| 2 | name python-performance-optimization |
| 3 | description Profile and optimize Python code using cProfile, memory profilers, and performance best practices. Use when debugging slow Python code, optimizing bottlenecks, or improving application performance. |
| 4 | |
| 5 | |
| 6 | # Python Performance Optimization |
| 7 | |
| 8 | Comprehensive guide to profiling, analyzing, and optimizing Python code for better performance, including CPU profiling, memory optimization, and implementation best practices. |
| 9 | |
| 10 | ## When to Use This Skill |
| 11 | |
| 12 | Identifying performance bottlenecks in Python applications |
| 13 | Reducing application latency and response times |
| 14 | Optimizing CPU-intensive operations |
| 15 | Reducing memory consumption and memory leaks |
| 16 | Improving database query performance |
| 17 | Optimizing I/O operations |
| 18 | Speeding up data processing pipelines |
| 19 | Implementing high-performance algorithms |
| 20 | Profiling production applications |
| 21 | |
| 22 | ## Core Concepts |
| 23 | |
| 24 | ### 1. Profiling Types |
| 25 | |
| 26 | **CPU Profiling**: Identify time-consuming functions |
| 27 | **Memory Profiling**: Track memory allocation and leaks |
| 28 | **Line Profiling**: Profile at line-by-line granularity |
| 29 | **Call Graph**: Visualize function call relationships |
| 30 | |
| 31 | ### 2. Performance Metrics |
| 32 | |
| 33 | **Execution Time**: How long operations take |
| 34 | **Memory Usage**: Peak and average memory consumption |
| 35 | **CPU Utilization**: Processor usage patterns |
| 36 | **I/O Wait**: Time spent on I/O operations |
| 37 | |
| 38 | ### 3. Optimization Strategies |
| 39 | |
| 40 | **Algorithmic**: Better algorithms and data structures |
| 41 | **Implementation**: More efficient code patterns |
| 42 | **Parallelization**: Multi-threading/processing |
| 43 | **Caching**: Avoid redundant computation |
| 44 | **Native Extensions**: C/Rust for critical paths |
| 45 | |
| 46 | ## Quick Start |
| 47 | |
| 48 | ### Basic Timing |
| 49 | |
| 50 | |
| 51 | import time |
| 52 | |
| 53 | def measure_time(): |
| 54 | """Simple timing measurement.""" |
| 55 | start = time.time() |
| 56 | |
| 57 | # Your code here |
| 58 | result = sum(range(1000000)) |
| 59 | |
| 60 | elapsed = time.time() - start |
| 61 | print(f"Execution time: {elapsed:.4f} seconds") |
| 62 | return result |
| 63 | |
| 64 | # Better: use timeit for accurate measurements |
| 65 | import timeit |
| 66 | |
| 67 | execution_time = timeit.timeit( |
| 68 | "sum(range(1000000))", |
| 69 | number=100 |
| 70 | ) |
| 71 | print(f"Average time: {execution_time/100:.6f} seconds") |
| 72 | |
| 73 | |
| 74 | ## Detailed patterns and worked examples |
| 75 | |
| 76 | Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient. |
| 77 | |
| 78 | ## Best Practices |
| 79 | |
| 80 | **Profile before optimizing** - Measure to find real bottlenecks |
| 81 | **Focus on hot paths** - Optimize code that runs most frequently |
| 82 | **Use appropriate data structures** - Dict for lookups, set for membership |
| 83 | **Avoid premature optimization** - Clarity first, then optimize |
| 84 | **Use built-in functions** - They're implemented in C |
| 85 | **Cache expensive computations** - Use lru_cache |
| 86 | **Batch I/O operations** - Reduce system calls |
| 87 | **Use generators** for large datasets |
| 88 | **Consider NumPy** for numerical operations |
| 89 | **Profile production code** - Use py-spy for live systems |
| 90 | |
| 91 | ## Common Pitfalls |
| 92 | |
| 93 | Optimizing without profiling |
| 94 | Using global variables unnecessarily |
| 95 | Not using appropriate data structures |
| 96 | Creating unnecessary copies of data |
| 97 | Not using connection pooling for databases |
| 98 | Ignoring algorithmic complexity |
| 99 | Over-optimizing rare code paths |
| 100 | Not considering memory usage |
| 101 |
Details
- Updated
- Sep 21, 2026
- License
- MIT
- Stars on the repo
- ★ 39,857
- Checked
- Source
- wshobson/agents ↗
- Creator
- wshobson · 179 listings
Similar skills
See all →
SkillAd Performance Analyzer
SkillQuality Constraint Manager
SkillObservability Instrumentation
SkillApplication Performance Optimizer
SkillAgent Skill Invocation
SkillPharmacokinetic Modeling SpecialistMore from wshobson
Skill.NET Backend Developer
SkillWCAG 2.2 Implementation
SkillMaterial Design 3 & Compose
SkillAngularJS to Angular Upgrade
SkillReverse Engineering Defense
SkillApache Airflow DAG Builder