
This walks you through the essential Python profiling toolkit, from cProfile for CPU bottlenecks to memory_profiler for tracking allocations and py-spy for production debugging. You'll learn to identify slow functions, optimize list comprehensions versus loops, fix string concatenation performance, and choose the right data structures. The patterns cover real optimization wins like switching from O(n) list searches to O(1) dictionary lookups, using generators to reduce memory usage, and leveraging local variables over globals. Solid for debugging sluggish applications or optimizing data processing pipelines before they hit production.
$ npx -y skills add wshobson/agents --skill python-performance-optimization --agent claude-codeInstalls into .claude/skills of the current project.
Comprehensive guide to profiling, analyzing, and optimizing Python code for better performance, including CPU profiling, memory optimization, and implementation best practices.
import time
def measure_time():
"""Simple timing measurement."""
start = time.time()
# Your code here
result = sum(range(1000000))
elapsed = time.time() - start
print(f"Execution time: {elapsed:.4f} seconds")
return result
# Better: use timeit for accurate measurements
import timeit
execution_time = timeit.timeit(
"sum(range(1000000))",
number=100
)
print(f"Average time: {execution_time/100:.6f} seconds")
Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.
Sponsors