python-performance-optimization

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

Updated Nov 30, 2025
One-click install
npx skills add https://github.com/Amakaflow/amakaflow-dev-workspace --skill python-performance-optimization-amakaflow
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-performance-optimization
Source: https://github.com/Amakaflow/amakaflow-dev-workspace/tree/main/.claude/skills/python-performance-optimization
Command: npx skills add https://github.com/Amakaflow/amakaflow-dev-workspace --skill python-performance-optimization-amakaflow

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve? Slow Python code and hidden memory leaks are hard to diagnose without proper tooling. This Skill provides a systematic approach to profiling CPU and memory usage, identifying bottlenecks, and applying proven optimization patterns. ## Core Features & Use Cases - CPU and Memory Profiling: Use cProfile, line_profiler, memory_profiler, and py-spy to pinpoint slow functions and memory leaks in development or production. - Optimization Patterns: Apply 20 concrete patterns covering list comprehensions, generators, caching with lru_cache, NumPy vectorization, multiprocessing, async I/O, and batch database operations. - Use Case: Imagine a data processing API endpoint that takes 10 seconds per request. Use this Skill to profile it with cProfile, discover a slow list-search loop, replace it with a dictionary lookup, and cut response time dramatically. ## Quick Start Profile my Python script to find the slowest functions and suggest optimizations to reduce its execution time.

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 slow functions?

Use cProfile to profile Python code by wrapping execution with profiler.enable() and profiler.disable(), then print sorted stats with pstats. For command-line use, run python -m cProfile -o output.prof script.py and inspect results with pstats.

What is the difference between cProfile and line_profiler?

cProfile measures time per function call across the whole program, while line_profiler reports execution time for each individual line inside a function. Use cProfile to find slow functions first, then line_profiler to pinpoint the exact lines causing the bottleneck.

How do I detect memory leaks in Python applications?

Use tracemalloc to take memory snapshots before and after running code, then compare them to find the largest allocations. The memory_profiler package with its @profile decorator also shows per-line memory consumption when running python -m memory_profiler script.py.

Can I profile a running Python process in production?

Yes, py-spy attaches to a running Python process by PID without modifying code. Use py-spy top --pid for live monitoring, py-spy record -o profile.svg --pid to generate flamegraphs, or py-spy dump to capture the current call stack.

When should I use multiprocessing versus async I/O in Python?

Use multiprocessing for CPU-bound tasks like heavy calculations, since it bypasses the GIL by running 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 string concatenation slow in loops?

Repeated string concatenation with += creates a new string object each iteration, causing quadratic behavior. Use str.join() with a list or generator of parts instead, which allocates the final string once and runs significantly faster.