Time and benchmarking¶
Level: 201 · working knowledge
One line: Rust has two clocks where C++ has three, one Duration where C++ has a type per unit, and a benchmarking hint the optimizer is asked — not told — to respect; this section is the Rust twin of the first chapter of the C++ learning library ↗.
Both chapters follow Matt Godbolt's C++Now 2026 keynote, Benchmarking: It's About Time ↗ (slides ↗). The C++ pages measure std::chrono; these measure std::time. Each lesson links its twin, so either language can come first.
| Lesson | What it answers | C++ twin |
|---|---|---|
Two clocks: Instant and SystemTime |
Which clock answers how long and which answers what time, what each one promises, and why C++'s third clock has no Rust counterpart | Three clocks ↗ |
An Instant is not a SystemTime |
The talk's Type safety slide in Rust — and the one subtraction Rust refuses that C++ allows | A time point knows its clock ↗ |
A Duration cannot be negative |
One unsigned type for every unit, and what you get instead when a subtraction would go below zero | A duration is a count and a unit ↗ |
| Timing a block | Instant::now() and elapsed(), why never SystemTime, and what a single timing cannot tell you |
Timing a block ↗ |
black_box is a hint |
What std::hint::black_box protects — a value, not the work that produced it — and why C++ has no standard answer at all |
The optimizer deletes your benchmark ↗ |
The order is the order of dependence: the second page subtracts the two types the first one introduces, the third is the type every subtraction produces, and the last two spend all three.
What this section does not cover¶
The talk goes further than std does, and so does the C++ chapter ↗:
- What a clock is. C++ defines a clock as a set of requirements, Cpp17Clock, that any type can meet, and the C++ chapter gives that a lesson of its own. Rust's std has no
Clocktrait —InstantandSystemTimeare two unrelated structs — so that lesson has no twin here. - The rest of the talk, which the C++ chapter carries as outlines: the vDSO that lets
clock_gettimeanswer without entering the kernel,rdtsc, what asking the time costs, branch predictors, caches, and the statistics that turn many timings into one measurement.
The last of those is what a benchmarking crate does for you in Rust. Criterion ↗ runs the code many times and reports the spread; every example in this library compiles with rustc alone, so none of them can use it. The method is written down in the Rust Performance Book's benchmarking chapter ↗.
See also¶
- What the optimizer does — constant folding, the reason a benchmark can measure nothing
- Operators are traits — why
Instant - Instantis allowed to be aDuration - C and C++: the bugs Rust is a reply to — the library's other chapter written against C++
- Going deeper — the performance and benchmarking shelf
Po polsku¶
Ten dział to rustowy odpowiednik pierwszego rozdziału biblioteki C++; oba opierają się na tym samym wykładzie Matta Godbolta z C++Now 2026. Temat jest jeden — pomiar czasu (timing) — ale Rust rozkłada go inaczej niż C++: ma dwa zegary zamiast trzech, jeden typ na czas trwania (duration) zamiast osobnego typu dla każdej jednostki i funkcję std::hint::black_box, która jest dla optymalizatora podpowiedzią (hint), a nie rozkazem.
Najważniejsza para pojęć: Instant to po polsku po prostu chwila — punkt w czasie, który można tylko porównać z innym albo od niego odjąć — odczytana z zegara monotonicznego (monotonic clock), który nigdy się nie cofa. SystemTime to czas systemowy (system time, potocznie wall-clock time): data i godzina, które administrator, NTP albo użytkownik mogą przestawić, także wstecz. Z tej jednej różnicy wynika reszta działu — łącznie z tym, że Rust nie skompiluje nawet SystemTime - SystemTime.
Dział kończy się tam, gdzie kończy się biblioteka standardowa. Reszta wykładu — koszt samego pytania o godzinę, rdtsc, pamięci podręczne (cache), statystyka wielu pomiarów — jest w bibliotece C++, a po stronie Rusta w crate'ach takich jak Criterion.
Szukaj po polsku: pomiar czasu w Ruscie · zegar monotoniczny · czas uniksowy · rust Instant SystemTime · rust benchmark black_box