performance-profiling

Profile Python CPU and memory bottlenecks using cProfile, line_profiler, memory_profiler, and py-spy.

Updated Sep 8, 2025
One-click install
npx skills add https://github.com/randalmurphal/claude-config --skill performance-profiling-randalmurphal
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: performance-profiling
Source: https://github.com/randalmurphal/claude-config/tree/main/skills/performance-profiling
Command: npx skills add https://github.com/randalmurphal/claude-config --skill performance-profiling-randalmurphal

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires snakeviz, line_profiler, memory_profiler, py-spy.

What problem does it solve?

This Skill provides a comprehensive guide to Python performance profiling, enabling developers to accurately identify and resolve CPU and memory bottlenecks. It helps transform slow, inefficient code into fast, optimized applications, ensuring better user experience and reduced infrastructure costs.

Core Features & Use Cases

  • CPU Profiling (cProfile, line_profiler, py-spy): Analyze function-level and line-level CPU usage to find hot spots and inefficient algorithms.
  • Memory Profiling (memory_profiler, tracemalloc): Detect memory leaks, identify large allocations, and understand memory growth patterns.
  • Production-Safe Profiling (py-spy): Profile live applications with minimal overhead, generating flame graphs for visual analysis.
  • Use Case: A Python API endpoint is experiencing slow response times. Use this skill to guide you through using cProfile to identify the slowest function, then line_profiler to pinpoint the exact line of code causing the bottleneck, and finally suggest optimization strategies.

Quick Start

Use the performance-profiling skill to guide me through profiling a Python script named my_app.py to find its CPU bottlenecks.

Frequently Asked Questions about performance-profiling

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I identify CPU bottlenecks in my Python application?

CPU bottlenecks are identified using profiling tools like cProfile, line_profiler, and py-spy. These tools measure function-level and line-level execution time, revealing which code sections consume the most processing power and where optimization efforts yield the greatest gains.

What's the difference between cProfile, line_profiler, and py-spy for Python profiling?

cProfile provides function-level CPU usage overview; line_profiler pinpoints exact lines consuming time within functions; py-spy profiles live production applications with minimal overhead and generates flame graphs. Choose based on whether you need broad overview, precise line analysis, or production-safe monitoring.

How do I detect memory leaks and track memory growth in Python?

Memory profiling uses memory_profiler and tracemalloc to detect leaks, identify large allocations, and understand memory growth patterns. These tools reveal which code sections allocate memory unexpectedly, helping you fix inefficiencies before they impact production systems.

Can I profile a running Python application without stopping it?

Yes, py-spy enables production-safe profiling of live applications with minimal overhead. It generates flame graphs for visual analysis without requiring code instrumentation or application restart, making it ideal for monitoring real-world workloads.

What do cumtime and tottime mean in profiling output?

tottime is the time spent executing a function's code alone; cumtime includes time spent in functions it calls. Analyzing both metrics helps distinguish between functions that are individually slow versus functions that call expensive dependencies.

Should I profile in development or production environments?

Profile in production-like conditions to capture realistic performance characteristics. Use py-spy for live production systems; use cProfile and line_profiler in staging environments that mirror production load, dependencies, and data volumes for accurate bottleneck detection.