Master the fundamental concepts of profiling & measurement through this focused micro-challenge.
clock_gettime(CLOCK_MONOTONIC) is what glibc, LLVM's benchmark harness, and the Linux kernel's own microbenchmarks use for sub-millisecond timing. Unlike gettimeofday, monotonic clocks never jump backward when NTP adjusts wall time. Pairing start and end reads around a tight loop is the foundation of every custom benchmark harness.
Read timespec at region start and end, subtract, convert to nanoseconds or microseconds. Warm up caches before the timed region so cold-start effects do not dominate.
cLoading…
CLOCK_PROCESS_CPUTIME_ID measures CPU time, not sleep timeKeep the relevant documentation open while you implement. When your output disagrees with the reference, trace one failing case by hand before changing random lines.
You will implement now_ns() and wrap a benchmark region with warmup plus repeated samples. This exercise asks you to report elapsed time for a known workload and verify results are stable across runs.
Implement high-resolution timer using clock_gettime.
Requirements:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github