Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Part of a 72-plugin marketplace with 112 AI agents and 146 skills for Claude Code development automation.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
SKILL.md
1---2name: python-performance-optimization3description: 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---56# Python Performance Optimization78Comprehensive guide to profiling, analyzing, and optimizing Python code for better performance, including CPU profiling, memory optimization, and implementation best practices.910## When to Use This Skill1112- Identifying performance bottlenecks in Python applications13- Reducing application latency and response times14- Optimizing CPU-intensive operations15- Reducing memory consumption and memory leaks16- Improving database query performance17- Optimizing I/O operations18- Speeding up data processing pipelines19- Implementing high-performance algorithms20- Profiling production applications2122## Core Concepts2324### 1. Profiling Types2526- **CPU Profiling**: Identify time-consuming functions27- **Memory Profiling**: Track memory allocation and leaks28- **Line Profiling**: Profile at line-by-line granularity29- **Call Graph**: Visualize function call relationships3031### 2. Performance Metrics3233- **Execution Time**: How long operations take34- **Memory Usage**: Peak and average memory consumption35- **CPU Utilization**: Processor usage patterns36- **I/O Wait**: Time spent on I/O operations3738### 3. Optimization Strategies3940- **Algorithmic**: Better algorithms and data structures41- **Implementation**: More efficient code patterns42- **Parallelization**: Multi-threading/processing43- **Caching**: Avoid redundant computation44- **Native Extensions**: C/Rust for critical paths4546## Quick Start4748### Basic Timing4950```python51import time5253def measure_time():54"""Simple timing measurement."""55start = time.time()5657# Your code here58result = sum(range(1000000))5960elapsed = time.time() - start61print(f"Execution time: {elapsed:.4f} seconds")62return result6364# Better: use timeit for accurate measurements65import timeit6667execution_time = timeit.timeit(68"sum(range(1000000))",69number=10070)71print(f"Average time: {execution_time/100:.6f} seconds")72```7374## Detailed patterns and worked examples7576Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.7778## Best Practices79801. **Profile before optimizing** - Measure to find real bottlenecks812. **Focus on hot paths** - Optimize code that runs most frequently823. **Use appropriate data structures** - Dict for lookups, set for membership834. **Avoid premature optimization** - Clarity first, then optimize845. **Use built-in functions** - They're implemented in C856. **Cache expensive computations** - Use lru_cache867. **Batch I/O operations** - Reduce system calls878. **Use generators** for large datasets889. **Consider NumPy** for numerical operations8910. **Profile production code** - Use py-spy for live systems9091## Common Pitfalls9293- Optimizing without profiling94- Using global variables unnecessarily95- Not using appropriate data structures96- Creating unnecessary copies of data97- Not using connection pooling for databases98- Ignoring algorithmic complexity99- Over-optimizing rare code paths100- Not considering memory usage101