C++ Academy · Lesson

Profiling with perf vtune and Sanitizers

Profile programs with perf and Intel VTune and catch bugs with AddressSanitizer.

Lesson 3 of 415 steps

Profiling with perf vtune and Sanitizers is a free C++ Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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 report

perf stat

Print summary metrics — cycles, instructions, cache misses, branch mispredictions — for a run.

perf stat -e cycles,instructions,cache-misses,branch-misses ./main

Flame 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.svg

macOS: 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 bug

Compile-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:

  1. Measure to identify the bottleneck
  2. Form a hypothesis
  3. Change one thing
  4. 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.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Profiling with perf vtune and Sanitizers” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Profiling with perf vtune and Sanitizers”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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

  1. Cache-Friendly Data Layouts
  2. Branch Prediction and Hot Loops
  3. Profiling with perf vtune and Sanitizers
  4. Micro-benchmarking with Google Benchmark
← Back to C++ Academy