| name | profile |
| description | Performance Profiling Guide |
| lifecycle | experimental |
/profile - Performance Profiling Guide
Profile code performance and identify bottlenecks.
Usage
/profile path/to/script.py # Profile Python script
/profile --memory # Memory profiling
/profile --flame # Generate flame graph
/profile cargo run # Profile Rust binary
What This Skill Does
- Run Profiler - CPU, memory, or I/O profiling
- Analyze Results - Identify hotspots
- Visualize - Flame graphs, call trees
- Recommend Fixes - Optimization suggestions
- Verify Improvement - Before/after comparison
Python Profiling
CPU Profiling (cProfile)
python -m cProfile -o profile.stats script.py
python -c "
import pstats
p = pstats.Stats('profile.stats')
p.sort_stats('cumulative').print_stats(20)
"
Line-by-Line (line_profiler)
@profile
def slow_function():
...
Memory Profiling (memory_profiler)
from memory_profiler import profile
@profile
def memory_heavy():
...
Async Profiling (py-spy)
py-spy record -o profile.svg --pid 12345
py-spy record -o profile.svg -- python script.py
Rust Profiling
CPU (perf + flamegraph)
cargo install flamegraph
cargo flamegraph --bin myapp
perf record --call-graph=dwarf ./target/release/myapp
perf report
Memory (heaptrack)
heaptrack ./target/release/myapp
heaptrack_gui heaptrack.myapp.*.gz
Profile Report Format
# Performance Profile: [Script/Binary]
## Summary
| Metric | Value |
|--------|-------|
| Total Time | 5.23s |
| Peak Memory | 256 MB |
| Hottest Function | `process_data` (45%) |
## Top 10 Functions by Time
| Function | Calls | Total Time | % |
|----------|-------|------------|---|
| process_data | 1000 | 2.35s | 45% |
| parse_json | 5000 | 1.12s | 21% |
| db_query | 100 | 0.89s | 17% |
## Flame Graph

## Bottleneck Analysis
### 1. `process_data` - 45% of time
**Issue**: Nested loops with O(n²) complexity
**Location**: module.py:123
**Current**:
```python
for item in items:
for other in items: # O(n²)
if item.matches(other):
...
Suggested Fix:
item_index = {item.key: item for item in items}
for item in items:
if item.key in item_index:
...
Expected Improvement: ~10x for large datasets
Memory Issues
Large Allocation: load_all_data
Issue: Loading entire file into memory
Fix: Use streaming/chunked reading
Recommendations
- High Impact: Optimize
process_data loop
- Medium Impact: Cache
parse_json results
- Low Impact: Batch
db_query calls
## Instructions for Claude
When /profile is invoked:
1. **Identify target** - Script, function, or binary
2. **Choose profiler** - CPU, memory, or both
3. **Run profiling** - Capture performance data
4. **Analyze results** - Find hotspots
5. **Generate visualizations** - Flame graphs if possible
6. **Identify bottlenecks** - Top time consumers
7. **Analyze complexity** - O(n) issues
8. **Suggest optimizations** - Concrete code changes
9. **Estimate improvement** - Expected speedup