Site Tools


wiki:hpp-chrono

Table of Contents

<chrono>

<chrono> provides time utilities: std::chrono::system_clock (wall time), std::chrono::steady_clock (monotonic, never goes backwards), and std::chrono::high_resolution_clock (highest precision available). You work with duration (a time interval) and time_point (a specific moment).

For benchmarking or measuring elapsed time, always use steady_clock. For absolute dates, use system_clock.

Example

This example measures the elapsed time of a sleep operation using steady_clock and converts the duration to milliseconds.

// compile: g++ -std=c++11 -o chronoexample chronoexample.cpp
// run: ./chronoexample
// description: measure elapsed time
 
#include <chrono>
#include <iostream>
#include <thread>
 
int main() {
    auto start = std::chrono::steady_clock::now();
 
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
 
    auto end = std::chrono::steady_clock::now();
    auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
 
    std::cout << "elapsed: " << elapsed_ms.count() << " ms\n";
 
    return 0;
}
wiki/hpp-chrono.md · Last modified: by 127.0.0.1