home / skills / dkyazzentwatwa / chatgpt-skills / code-profiler

code-profiler skill

/code-profiler

This skill profiles Python code performance, identifies bottlenecks, and validates optimizations with timing, memory, and call-graph insights.

npx playbooks add skill dkyazzentwatwa/chatgpt-skills --skill code-profiler

Review the files below or copy the command above to add this skill to your agents.

Files (3)
SKILL.md
1.4 KB
---
name: code-profiler
description: Use when asked to profile Python code performance, identify bottlenecks, measure execution time, or analyze function call statistics.
---

# Code Profiler

Analyze Python code performance, identify bottlenecks, and optimize execution with comprehensive profiling tools.

## Purpose

Performance analysis for:
- Bottleneck identification
- Function execution time measurement
- Memory usage profiling
- Call graph visualization
- Optimization validation

## Features

- **Time Profiling**: Measure function execution times
- **Line-by-Line Analysis**: Profile each line of code
- **Call Statistics**: Function call counts and cumulative time
- **Memory Profiling**: Track memory allocation and usage
- **Flamegraph Visualization**: Visual call stack analysis
- **Comparison**: Before/after optimization comparison

## Quick Start

```python
from code_profiler import CodeProfiler

# Profile function
profiler = CodeProfiler()
profiler.profile_function(my_function, args=(arg1, arg2))
profiler.print_stats(top=10)

# Profile script
profiler.profile_script('script.py')
profiler.export_report('profile_report.html')
```

## CLI Usage

```bash
# Profile Python script
python code_profiler.py script.py

# Profile with line-by-line analysis
python code_profiler.py script.py --line-by-line

# Export HTML report
python code_profiler.py script.py --output report.html
```

Overview

This skill profiles Python code to find performance bottlenecks, measure execution time, and validate optimizations. It combines time profiling, line-by-line analysis, memory tracking, and visual outputs like flamegraphs and HTML reports. Use it to get actionable metrics that guide targeted refactoring and performance tuning.

How this skill works

The profiler runs functions or scripts under instrumentation to collect timing, call counts, and memory allocation data. It can produce aggregate statistics, line-level timings, call graphs, and flamegraphs, and export human-readable HTML reports for comparison. You can profile a specific function, a full script, or compare before/after runs to validate improvements.

When to use it

  • When a function or endpoint is significantly slower than expected
  • To identify hot lines in CPU-bound code using line-by-line analysis
  • When memory usage spikes or leaks are suspected
  • Before and after optimizations to validate performance gains
  • When you need visual call-stack analysis (flamegraphs) for complex code paths

Best practices

  • Profile realistic inputs and workloads to get representative results
  • Isolate the code under test to reduce noise from unrelated modules
  • Run multiple iterations and compare median timings rather than a single run
  • Use line-by-line profiling only after narrowing expensive functions to reduce overhead
  • Export reports and flamegraphs to share results with teammates

Example use cases

  • Measure and reduce latency of a web request handler by profiling the handler function
  • Find the slowest lines in a data-processing loop to target vectorization or caching
  • Track memory allocation in a long-running service to identify leaks
  • Compare execution time and call counts before and after replacing an algorithm
  • Generate an HTML report and flamegraph to review performance with the engineering team

FAQ

Can I profile a single function without running a whole script?

Yes. You can directly profile a function with provided helpers that accept the target function and its arguments.

How do I reduce profiling overhead for long-running programs?

Profile representative short runs or sample-based profiling; use line-by-line only for isolated hotspots to minimize overhead.

Does it support visual outputs?

Yes. The skill can produce flamegraphs and export HTML reports for easy visualization and sharing.