python-performance-optimization

Profile Python applications for CPU and memory bottlenecks using cProfile and py-spy.

Updated Apr 17, 2026
One-click install
npx skills add https://github.com/Chris-Maskey/opencode-config --skill python-performance-optimization-chris-maskey
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-performance-optimization
Source: https://github.com/Chris-Maskey/opencode-config/tree/main/skills/python-performance-optimization
Command: npx skills add https://github.com/Chris-Maskey/opencode-config --skill python-performance-optimization-chris-maskey

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Pinpoints CPU and memory hotspots and provides practical profiling and optimization workflows so engineers can reduce latency, lower memory consumption, and improve throughput in Python applications.

Core Features & Use Cases

  • CPU profiling guidance using cProfile and pstats to surface slow functions and cumulative costs.
  • Production-safe sampling with py-spy and flamegraph generation for live-process diagnosis.
  • Line-by-line and memory allocation analysis using line_profiler, memory_profiler, and tracemalloc to detect leaks and heavy allocations.
  • Optimization patterns and examples: list comprehensions, generators, local variable access, lru_cache, NumPy vectorization, multiprocessing, and async I/O.
  • Benchmarking and validation guidance including custom decorators and pytest-benchmark to measure improvements before/after changes.

Quick Start

Profile the Python script main.py for CPU and memory hotspots, save a cProfile output, generate a flamegraph with py-spy, and list the top optimization targets.

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 scripts to find CPU and memory bottlenecks?

To profile Python scripts for bottlenecks, you can use cProfile to identify slow functions and tracemalloc to detect heavy memory allocations. This process pinpoints CPU and memory hotspots, enabling targeted optimizations to reduce latency and lower memory consumption.

Can I profile a live production Python process without causing downtime?

Yes, you can profile a live production Python process safely using py-spy. This sampling profiler performs production-safe sampling on live processes, allowing you to generate flamegraphs and diagnose CPU bottlenecks without stopping or restarting your application.

What is the best way to analyze memory leaks in a Python application?

The best way to analyze memory leaks in Python is by using memory_profiler and tracemalloc for line-by-line allocation analysis. These tools detect leaks and heavy allocations, providing practical workflows to lower memory consumption and resolve memory-bound performance issues.

Does this Python optimization guidance work for data pipelines and web backends?

Yes, this Python optimization guidance applies to command-line scripts, web backends, data pipelines, and production processes. It addresses CPU-bound, memory-bound, and I/O-bound performance issues across these environments using profiling and optimization patterns like NumPy vectorization and async I/O.

How do I validate Python performance improvements after optimizing code?

To validate Python performance improvements, you can use benchmarking and validation guidance with custom decorators and pytest-benchmark. This allows you to measure throughput and latency before and after changes, ensuring your optimizations like lru_cache or multiprocessing effectively improve performance.