python-performance-optimization

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

Updated Apr 13, 2026
One-click install
npx skills add https://github.com/scoots31/engineering-playbook --skill python-performance-optimization-scoots31
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-performance-optimization
Source: https://github.com/scoots31/engineering-playbook/tree/main/references/python-performance-optimization
Command: npx skills add https://github.com/scoots31/engineering-playbook --skill python-performance-optimization-scoots31

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires line-profiler, memory-profiler, py-spy, and includes references (resource) components.

What problem does it solve? Slow Python code and hidden memory leaks are hard to diagnose without measurement. This Skill provides a systematic approach to profiling CPU time, memory usage, and line-level hotspots, then applying proven optimization patterns to eliminate bottlenecks. ## Core Features & Use Cases - CPU and Line Profiling: Use cProfile, pstats, and line_profiler to identify time-consuming functions and exact slow lines. - Memory Analysis: Track allocations with memory_profiler and profile live production processes with py-spy flamegraphs. - Optimization Patterns: Apply concrete techniques like list comprehensions, generators, dict lookups, string joins, and local variable access with measurable speedups. - Use Case: A data processing pipeline takes 10 minutes to run. Profile it with cProfile to find the hot function, switch a list search to a dict lookup, and cut runtime by an order of magnitude. ## Quick Start Profile my Python script with cProfile and show me the top 10 slowest functions, then suggest 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 Python code to find slow functions?

Use cProfile to profile Python code by wrapping execution with profiler.enable() and profiler.disable(), then print results with pstats sorted by cumulative time. You can also run python -m cProfile -o output.prof script.py from the command line.

How do I find memory leaks in a Python application?

Use memory_profiler with the @profile decorator to track line-by-line memory allocation in a function. Run the script with python -m memory_profiler script.py to see where memory grows and identify leaks.

What is the difference between cProfile and line_profiler?

cProfile measures time at the function level, showing call counts and cumulative time per function. line_profiler measures execution time for each individual line, giving finer granularity when you already know which function is slow.

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 for live monitoring or py-spy record -o profile.svg --pid <pid> to generate a flamegraph.

Why are list comprehensions faster than for loops in Python?

List comprehensions avoid repeated append method lookups and are optimized in the interpreter's C implementation. Benchmarks with timeit typically show comprehensions running measurably faster than equivalent loop-and-append code.

When should I use generators instead of lists in Python?

Use generators when processing large datasets where you only need one item at a time. Generators yield values lazily and use constant memory regardless of dataset size, while lists store all elements in memory.