Chrono and Formatting

Chrono and Formatting

Durations, clocks, time points, and modern text formatting in one quick reference.

Chrono and Formatting

Durations and clocks

using namespace std::chrono_literals;

auto timeout = 250ms;
auto start = std::chrono::steady_clock::now();

Measuring elapsed time

auto start = std::chrono::steady_clock::now();
do_work();
auto end = std::chrono::steady_clock::now();
auto elapsed = end - start;

Converting durations

auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed);

std::format and std::print

auto msg = std::format("{} took {} ms", name, ms.count());
std::print("{}\n", msg);

Practical guidance