C++ Date and time using header Measuring time using


Example

The system_clock can be used to measure the time elapsed during some part of a program's execution.

c++11
#include <iostream>
#include <chrono>
#include <thread>

int main() {
    auto start = std::chrono::system_clock::now(); // This and "end"'s type is std::chrono::time_point
    { // The code to test
        std::this_thread::sleep_for(std::chrono::seconds(2));
    }
    auto end = std::chrono::system_clock::now();

    std::chrono::duration<double> elapsed = end - start;
    std::cout << "Elapsed time: " << elapsed.count() << "s";
}

In this example, sleep_for was used to make the active thread sleep for a time period measured in std::chrono::seconds, but the code between braces could be any function call that takes some time to execute.