python-profiling

Provides guidance for profiling and optimizing Python code performance and resource usage.

10|Updated Mar 9, 2026
One-click install
npx skills add https://github.com/fgmacedo/agent-skills --skill python-profiling
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-profiling
Source: https://github.com/fgmacedo/agent-skills/tree/main/skills/python-profiling
Command: npx skills add https://github.com/fgmacedo/agent-skills --skill python-profiling

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Speed issues and memory bloat in Python code are hard to fix without knowing where time and allocations are actually going, so this Skill provides a structured, benchmark-driven way to identify bottlenecks and validate improvements.

Core Features & Use Cases

  • CPU and memory profiling workflow: Choose the right tool for CPU-bound vs memory-bound problems and move from broad to precise diagnostics.
  • Benchmark-first optimization: Establish a baseline with pytest-benchmark (or ad-hoc cProfile) and require measurable before/after improvement after each single change.
  • Tool-driven investigation: Use cProfile for overview, line_profiler for per-line CPU hotspots, py-spy for low-intrusion sampling, tracemalloc for allocation tracking, and memray/scalene for deeper memory insights.
  • Use case example: When a service endpoint is “slow,” run a baseline benchmark, identify the hottest call paths, apply one optimization (e.g., reduce allocations or cache repeated work), then rerun benchmarks and the full test suite to ensure correctness.

Quick Start

Ask the agent to profile the Python code and reduce memory usage by running a baseline, identifying the bottleneck with the appropriate profiler(s), applying one optimization at a time, and validating the results with benchmarks and the full test suite.

Frequently Asked Questions about python-profiling

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

FAQPage Schema
How do I find CPU hotspots and memory bottlenecks in Python code?

Python profiling identifies CPU hotspots and memory allocation bottlenecks using a measurement-first approach. You select appropriate profilers for CPU versus memory issues, establish a reproducible baseline, and execute one optimization change at a time to confirm measurable improvement.

What's the best way to benchmark Python performance optimizations before and after changes?

Benchmark validation in Python establishes a baseline using pytest-benchmark or ad-hoc cProfile, applies a single optimization change, then reruns benchmarks to confirm measurable before-and-after improvement while ensuring correctness via the full test suite.

Which Python profiler should I use for CPU versus memory allocation analysis?

For CPU profiling in Python, use cProfile for an overview, line_profiler for per-line hotspots, and py-spy for low-intrusion sampling. For memory allocation tracking, use tracemalloc, memray, or scalene for deeper allocation insights.

Can I use pytest-benchmark workflows to fix slow Python service endpoints?

Yes, pytest-benchmark workflows support fixing slow Python service endpoints by running a baseline benchmark, identifying the hottest call paths with appropriate profilers, applying one optimization like reducing allocations, then rerunning benchmarks and tests.

Why does my Python optimization change not improve performance even after profiling?

Python profiling requires a measurement-first approach where you establish a reproducible baseline before optimizing. If performance does not improve, ensure you selected the correct profiler for CPU versus memory bottlenecks and applied only one optimization change at a time.

Do I need a reproducible baseline before optimizing Python code with profiling?

Yes, Python profiling requires establishing a reproducible baseline before making changes. This benchmark-first optimization ensures you can accurately measure before-and-after improvement and validate that each single optimization change produces a measurable performance gain.