Profiling with perf vtune and Sanitizers
Profile programs with perf and Intel VTune and catch bugs with AddressSanitizer.
Profiling with perf vtune and Sanitizers is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Profile?
Intuition about performance is usually wrong. Profile to find real bottlenecks — then optimize.
Linux: perf
perf is a low-overhead sampling profiler that ships with Linux. Run it on any binary built with debug info.
g++ -O2 -g main.cpp -o main
perf record ./main
perf reportperf stat
Print summary metrics — cycles, instructions, cache misses, branch mispredictions — for a run.
perf stat -e cycles,instructions,cache-misses,branch-misses ./mainFlame Graphs
Convert perf data into a flame graph showing where time is spent.
perf record -F 99 -g ./main
perf script | stackcollapse-perf.pl | flamegraph.pl > flame.svgmacOS: Instruments
Apple s Instruments provides time profiling, allocations, leaks, and more. Free with Xcode.
Windows: VTune Profiler
Intel VTune (free for personal use) offers detailed microarchitectural analysis — pipeline stalls, cache misses, hot blocks. Cross-platform.
Sanitizers Catch Bugs
Sanitizers are runtime checkers built into clang and gcc. Recompile with the appropriate flag:
-fsanitize=address— AddressSanitizer for memory bugs-fsanitize=thread— ThreadSanitizer for data races-fsanitize=undefined— UB Sanitizer for signed overflow, etc.-fsanitize=memory— MSan for uninitialized reads
AddressSanitizer Example
Catches buffer overflows, use-after-free, and double-free with stack traces.
g++ -fsanitize=address -g main.cpp
./a.out
# Output points exactly at the bugCompile-Time vs Run-Time
Static analysis (clang-tidy, cppcheck) finds bugs without running. Sanitizers find runtime bugs. Use both — they catch different issues.
Profiling Allocations
Heap allocations are slow. Use perf, Valgrind s massif, or Heaptrack to find hot allocation sites and reduce them.
Reading Profiles
Focus on the top functions in the profile. Then look up the call tree to understand context. A few percent here and there are usually not worth optimizing.
Continuous Profiling
For production, sample-based profilers like Pyroscope or Pixie collect data continuously with low overhead. Catch regressions before they hurt users.
Workflow
The optimization loop:
- Measure to identify the bottleneck
- Form a hypothesis
- Change one thing
- Measure again
Without measurement, you are guessing.
Quick Check
Which compiler flag enables AddressSanitizer?
Recap
Profile with perf (Linux), Instruments (macOS), or VTune (cross-platform) to find bottlenecks. Use sanitizers to catch memory and threading bugs early. Measure, hypothesize, change, measure again.
Frequently asked questions
Is the “Profiling with perf vtune and Sanitizers” lesson free?
Yes — the full text of “Profiling with perf vtune and Sanitizers” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Profiling with perf vtune and Sanitizers”?
Profile programs with perf and Intel VTune and catch bugs with AddressSanitizer. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Profiling with perf vtune and Sanitizers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Cache-Friendly Data Layouts
- Branch Prediction and Hot Loops
- Profiling with perf vtune and Sanitizers
- Micro-benchmarking with Google Benchmark