python-performance-optimization

Diagnose Python performance bottlenecks using cProfile, memory_profiler, and NumPy.

Updated Apr 5, 2026
One-click install
npx skills add https://github.com/Jhabbig/Habbig --skill python-performance-optimization-jhabbig
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-performance-optimization
Source: https://github.com/Jhabbig/Habbig/tree/main/.claude/plugins/wshobson/python-development/skills/python-performance-optimization
Command: npx skills add https://github.com/Jhabbig/Habbig --skill python-performance-optimization-jhabbig

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps you find and fix Python performance bottlenecks by turning slow, memory-heavy code into faster, more efficient implementations.

Core Features & Use Cases

  • CPU profiling with cProfile, pstats, and py-spy to pinpoint hot paths.
  • Memory profiling with memory_profiler and tracemalloc to catch leaks and excess allocation.
  • Optimization guidance for data structures, generators, caching, NumPy vectorization, async I/O, multiprocessing, and database batching.
  • Use case: Analyze a slow ETL job, identify the expensive functions, and rewrite the bottleneck so it runs faster and uses less memory.

Quick Start

Ask for a step-by-step Python performance review of your slow function or service, including profiling recommendations and code-level optimizations.

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 a slow Python function to find performance bottlenecks?

Profile Python performance bottlenecks using cProfile and pstats to measure hot paths and identify expensive functions. This line-level analysis targets CPU-heavy code sections so you can rewrite them for faster execution and reduced runtime.

What's the best way to reduce memory usage in a heavy Python ETL job?

Reduce Python memory usage by profiling allocations with memory_profiler and tracemalloc, then applying generators, caching, and database batching. This optimization catches memory leaks and excess allocations to improve overall efficiency.

How does NumPy vectorization improve Python performance?

NumPy vectorization improves Python performance by replacing slow loops with optimized array operations, reducing allocations and runtime. This technique targets computational bottlenecks identified during CPU profiling to make data processing faster.

Can I use py-spy to profile Python production services without stopping them?

Yes, py-spy samples CPU usage in Python production environments without requiring application restarts or code modifications. It measures hot paths in running services to diagnose performance bottlenecks during production troubleshooting.

When should I use async I/O versus multiprocessing for Python optimization?

Use async I/O for Python optimization when bottlenecks involve I/O-bound tasks like database queries, and multiprocessing for CPU-bound workloads. Profiling with cProfile determines whether hot paths are waiting on I/O or exhausting CPU resources.

Do I need cProfile and memory_profiler to optimize slow Python code?

Yes, cProfile and memory_profiler are required to measure CPU hot paths and memory allocations accurately. Profiling provides the baseline data needed to identify expensive functions and verify that optimizations actually improve runtime efficiency.