Performance Analysis

Timing a small loop like the one above gave nearly zero time even for large N. What is the most likely cause?

start = now();
for (i = 0; i < N; i++)
    x = a * b + i;
end = now();
  1. AThe CPU has a special instruction that finishes the computation instantly
  2. BBecause x is never used afterward, the compiler removed the loop computation as unnecessary
  3. CThe clock function is broken
Show answer and explanation

Answer: B. Because x is never used afterward, the compiler removed the loop computation as unnecessary

With optimization enabled, compilers remove computations whose results are unused as dead code, or compute loop results ahead of time and replace them with constants. So in microbenchmarks that isolate a small piece of code, the computation being measured may not actually run. Countermeasures include printing the result, using volatile or other means of defeating the optimization, and inspecting the generated machine code.

  • A. No such instruction exists.
  • B. Correct. It is a common microbenchmark pitfall.
  • C. The clock works correctly.
Practice this topic Back to topic

More Performance Analysis questions

  1. When timing a program, why are measurements often repeated with the first few runs discarded?
  2. What is the problem with timing a very short operation once, and what is the fix?
  3. What is the difference between wall-clock time and CPU time?
  4. Which correctly describes throughput and latency as server performance measures?
  5. When evaluating web service response times, why is the 99th percentile (the boundary of the slowest 1%) often examined in addition to the mean?