python-performance-optimization

Profile and optimize Python code using cProfile, memory profilers, and benchmarking patterns.

Updated Jul 29, 2026
One-click install
npx skills add https://github.com/MaiconGambini/opencode-harness-guide --skill python-performance-optimization-maicongambini
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-performance-optimization
Source: https://github.com/MaiconGambini/opencode-harness-guide/tree/main/skills/python-performance-optimization
Command: npx skills add https://github.com/MaiconGambini/opencode-harness-guide --skill python-performance-optimization-maicongambini

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires line-profiler, memory-profiler, py-spy, numpy, aiohttp, requests, pytest-benchmark.

What problem does it solve? Slow Python applications waste compute resources and frustrate users, but finding the real bottleneck requires systematic profiling rather than guesswork. This Skill provides a structured workflow for measuring CPU time, memory usage, and I/O wait, then applying proven optimization patterns. ## Core Features & Use Cases - CPU and Line Profiling: Use cProfile, line_profiler, and py-spy to identify time-consuming functions in development and production. - Memory Optimization: Track allocations with memory_profiler and tracemalloc, reduce footprint with generators, slots, and weak references. - Optimization Patterns: Apply list comprehensions, caching with lru_cache, NumPy vectorization, multiprocessing, async I/O, and batch database operations. - Use Case: A data processing pipeline takes 10 minutes per run. Profile it with cProfile to find the hot loop, replace list searches with dict lookups, and vectorize numerical work with NumPy to cut runtime significantly. ## Quick Start Profile my Python script to find the slowest functions and suggest concrete optimizations with benchmark comparisons.

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 measure function-level execution time, then sort results by cumulative time to find hot paths. For line-by-line detail, use line_profiler, and for running production processes, attach py-spy to generate flamegraphs without code changes.

How do I reduce memory usage in Python applications?

Replace lists with generators for large datasets, add __slots__ to classes with many instances, and use weakref-based caches so objects can be garbage collected. Track allocations with memory_profiler and tracemalloc to confirm reductions.

cProfile vs py-spy: which profiler should I use?

cProfile is built-in and gives deterministic function-level stats during development. py-spy is a sampling profiler that attaches to running processes with low overhead, making it suitable for profiling production systems without restarting them.

When should I use multiprocessing vs asyncio in Python?

Use multiprocessing for CPU-bound tasks like heavy calculations, since it bypasses the GIL with separate processes. Use asyncio with libraries like aiohttp for I/O-bound work such as concurrent HTTP requests, where tasks wait on network or disk.

Why is my Python optimization not improving performance?

Optimizing without profiling often targets the wrong code path. Measure first with cProfile or timeit, focus on hot loops and algorithmic complexity, and benchmark before and after each change to verify actual improvement.