name: profiling-analysis
description: Profile code performance — statistical profilers (perf, samply, flamegraph), hardware counters, benchmark analysis. Use when measuring optimization impact or validating that a change actually improved performance.
license: MIT
What I Do
Guide performance investigation using profiling tools and hardware counters. Use
when investigating why code is slow or validating optimization impact.
The Profiling Mindset
Why Profile?
"If you haven't measured, you haven't optimized."
Measure actual behavior, not assumed behavior
Find the real bottleneck, not where you think it is
Validate that optimizations actually worked
Rule of Thumb
Don't optimize what you haven't profiled. Profile → Optimize → Profile again
to verify.
Profiling Tools
Linux: perf
The standard Linux profiling tool:
# Basic CPU profiling
perf stat -e cycles,instructions,branches,branch-misses ./program
# Profile with call graph
perf record -g ./program
perf report
# Hardware counters
perf stat -e cache-references,cache-misses,branch-misses ./program
# Top hotspots
perf top -G
10x latency gap between L1 and RAM means cache misses are expensive.
Cache Access Patterns
Bad: Strided access
// Accesses every 4096 bytes = cache line torture
for (int i = 0; i < n; i += 512)
sum += a[i];
Good: Sequential access
// Sequential = prefetcher works
for (int i = 0; i < n; i++)
sum += a[i];
Branch Prediction Analysis
Misprediction Rates
perf stat -e branches,branch-misses ./program
Good rates:
< 1% for predictable branches
< 5% for semi-predictable
10% = consider branchless
Signs of Branch Mispredictions
High branch-misses / branches ratio
Code with many if-else in hot paths
Data-dependent branching
Branchless When
Branch is data-dependent (input decides taken/not-taken)
Branch is in tight loop
Branch is unpredictable
Memory Bandwidth
Measuring Bandwidth
# STREAM benchmark alternative - simple copy
perf stat -e mem_load_retired.l3_hit,mem_load_retired.l3_miss ./program
Roofline: Know Your Limits
DDR4-3200: ~25 GB/s per channel
AVX-512 memory bandwidth: ~100 GB/s
If your code does:
10 FLOPs per 64 bytes loaded = 160 GFLOPS (memory bound on DDR4)
100 FLOPs per 64 bytes = could hit compute roof
Benchmarking Best Practices
What to Measure
Wall-clock time - Actual elapsed time
CPU cycles - Machine cycles (more stable across machines)
Instructions - Number of ops executed
Cache misses - Memory behavior
Common Pitfalls
Benchmarking debug builds - Always use -O3 or equivalent
Single runs - Need statistical significance
Aliasing effects - Different CPUs behave differently
Thermal throttling - Let CPU cool between runs
Background processes - Close other apps
CPU frequency scaling - Disable turbo/powersave or use perf
Accurate Timing
// Rust: use std::time or criterion
use std::time::Instant;
let start = Instant::now();
work();
let elapsed = start.elapsed();
// Multiple runs
let runs = 1000;
let total: Duration = (0..runs)
.map(|_| {
let start = Instant::now();
work();
start.elapsed()
})
.sum();
let avg = total / runs;
Profiling Workflow
Step 1: Identify Hotspots
perf record -g ./program
perf report
Find where most time is spent.
Step 2: Analyze Why
Is it CPU-bound or memory-bound?
What's the cache miss rate?
Are branches mispredicting?
perf stat -e cycles,instructions,cache-misses,branch-misses ./program
Step 3: Optimize
Apply appropriate optimization based on analysis.
Step 4: Verify
Re-run profiling to confirm improvement.
Platform-Specific Tools
macOS
# Instruments (GUI)
open /Applications/Instruments.app
# Sample (CLI)
sample <pid>
Windows
VTune Profiler (Intel) - Free, excellent
Visual Studio Profiler - Built-in
Windows Performance Toolkit - WPR/WPA
All Platforms
Intel VTune - Works on AMD too
AMD μProf - AMD-specific
Google Benchmark - Portable microbenchmarking
Checklist
Profile before optimizing
Use multiple runs with statistical analysis
Identify CPU vs memory bound
Check cache miss rates
Check branch misprediction rates
Verify optimizations with re-profiling
Benchmark optimized vs unoptimized
Use With
Load these skills together for complete performance work: