Measuring time using chrono

suggest change

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

#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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents