Skip to content

The cost of asking the time

Level: 301 · deep dive

Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The bullets below are the questions the finished page has to answer.

One line: Reading a clock costs tens of nanoseconds — nothing next to a network call, and more than the thing you are measuring when that thing is a handful of instructions.

The questions this page has to answer

  • How long does one now() take, and how do you measure it without the numeric_limits<duration>::max() trap that printed Min = 0ns in the talk (A duration is a count and a unit)? The first numbers from this machine are on Three clocks.
  • The talk's table puts rdtsc at 14 ns, rdtscp at 23 ns and steady_clock::now() at 35 ns on its machine. How does the vDSO fit about 89 extra instructions into about 25 extra cycles?
  • Should you subtract the clock's overhead from each measurement, or amortise it by timing N runs at once?
  • Delta benchmarking: time N iterations and 2N, and subtract. Why does the overhead cancel, and what does it assume?
  • At what size of work is the clock's own cost larger than the thing being measured?

In the talk

What is now()?How long does it take?; and When will then be now()?The cost of asking the time, Accounting for the clock overhead and Delta benchmarking (slide source ↗).

See also