C++ · Advanced level

Advanced level · Module 4: Testing and Performance in C++ · Lesson 2 of 4

Measuring Performance

Measure before optimising: profile to find hot spots and benchmark changes reliably.

In this lesson you will

  • Profile a program to find where time is spent
  • Write repeatable benchmarks and avoid common measurement errors

Key concepts

profilerhot spotbenchmarkrelease build

The lesson

Programmers are poor at guessing where time goes. A profiler — perf on Linux, the Visual Studio profiler on Windows — samples the program and shows which functions use the most time. Optimise those, not the code you suspect.

Benchmark with optimised release builds, realistic data sizes and several repetitions, and report the median time. Make sure the compiler cannot remove the work being measured because its result is unused.

Keep a record: what changed, the data set, the machine and the before and after times. A claim of '30% faster' needs that evidence.

Step by step

  1. Build the processor in release mode.
  2. Profile it on the large data file.
  3. Identify the top three hot spots.
  4. Write a repeatable timing harness and record the baseline.

Practice activity

Produce a profile report naming the hottest function and a baseline benchmark for it.

Expected result: A report with profiler evidence and a repeatable baseline time.

Check your understanding

  1. Why profile before optimising?

    Show answer

    To find where time is really spent instead of guessing.

  2. Why measure with a release build?

    Show answer

    Debug builds have very different performance and would mislead you.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.