
Python Performance Optimization
Profile slow Python services and pipelines with cProfile and memory tools so solo builders fix bottlenecks before users feel the lag.
Overview
Python Performance Optimization is an agent skill most often used in Ship (also Operate, Build) that profiles and optimizes Python bottlenecks with cProfile, memory tools, and implementation best practices.
Install
npx skills add https://github.com/wshobson/agents --skill python-performance-optimizationWhat is this skill?
- CPU profiling with cProfile plus memory and line-level profilers
- Four profiling types: CPU, memory, line, and call-graph analysis
- Metrics coverage: execution time, memory, CPU utilization, and I/O wait
- Optimization strategies spanning algorithms, implementation, caching, and query tuning
- Triggers for data pipelines, production profiling, and leak investigation
- 4 profiling types: CPU, memory, line, and call graph
- 4 core performance metrics: execution time, memory, CPU utilization, I/O wait
Adoption & trust: 25.6k installs on skills.sh; 36.5k GitHub stars; 3/3 security scanners passed (skills.sh audits).
What problem does it solve?
Your Python app feels slow or memory-hungry but you do not know which functions, queries, or I/O paths deserve the first optimization pass.
Who is it for?
Solo builders shipping Python backends, CLIs, or batch pipelines who need systematic profiling before refactoring or scaling.
Skip if: Teams that only need generic code style linting, or workloads where performance is entirely delegated to managed serverless with no Python hot path to inspect.
When should I use this skill?
Debugging slow Python code, optimizing bottlenecks, or improving application performance.
What do I get? / Deliverables
You get a profiling-driven plan—CPU and memory hotspots identified—with algorithmic and implementation changes applied to cut latency and resource use.
- Profiling results identifying hotspot functions and lines
- Prioritized optimization changes for CPU, memory, and I/O
Recommended Skills
Journey fit
Spans multiple journey phases - primary shelf plus alternate fits below.
Performance work belongs on the Ship shelf because it gates release quality, even when you first notice slowness during Operate. Perf subphase is the canonical home for latency, CPU, memory, and I/O optimization workflows tied to shipping reliable apps.
Where it fits
Run CPU and memory profiles on staging before tagging a release when p95 latency fails your solo SLO.
Re-profile after a traffic spike or memory alert to isolate regressions in background workers.
Optimize ORM-heavy endpoints while implementing core API routes for the first MVP slice.
How it compares
Use instead of ad-hoc print-timing loops when you need structured cProfile and memory profiling workflows.
Common Questions / FAQ
Who is python-performance-optimization for?
Indie developers and small teams running Python in production or pre-launch who own response times and memory on their own metal or containers.
When should I use python-performance-optimization?
During Ship perf hardening before release, during Operate when monitoring shows regressions, and during Build when backend or pipeline code first shows CPU or memory pressure.
Is python-performance-optimization safe to install?
Review the Security Audits panel on this Prism page for install risk and repo trust signals before adding it to your agent environment.
SKILL.md
READMESKILL.md - Python Performance Optimization
# 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 ```python 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") ``` ## Profiling Tools ### Pattern 1: cProfile - CPU Profiling ```python import cProfile import pstats from pstats import SortKey def slow_function(): """Function to profile.""" total = 0 for i in range(1000000): total += i return total def another_function(): """Another function.""" return [i**2 for i in range(100000)] def main(): """Main function to profile.""" result1 = slow_function() result2 = another_function() return result1, result2 # Profile the code if __name__ == "__main__": profiler = cProfile.Profile() profiler.enable() main() profiler.disable() # Print stats stats = pstats.Stats(profiler) stats.sort_stats(SortKey.CUMULATIVE) stats.print_stats(10) # Top 10 functions # Save to file for later analysis stats.dump_stats("profile_output.prof") ``` **Command-line profiling:** ```bash # Profile a script python -m cProfile -o output.prof script.py # View results python -m pstats output.prof # In pstats: # sort cumtime # stats 10 ``` ### Pattern 2: line_profiler - Line-by-Line Profiling ```python # Install: pip install line-profiler # Add @profile decorator (line_profiler provides this) @profile def process_data(data): """Process data with line profiling.""" result = [] for item in data: processed = item * 2 result.append(processed) return result # Run with: # kernprof -l -v script.py ``` **Manual line profiling:** ```python from line_profiler import LineProfiler def process_data(data): """Function to profile.""" result = [] for item in data: processed = item * 2 result.append(processed) return result if __name__ == "__main__": lp = LineProfiler() lp.add_function(process_data) data = list(range(100000)) lp_wrapper = lp(p