std::chrono


Namespace: std::chrono

std::chrono is a C++ library that provides a comprehensive framework for dealing with time durations, time points, and clocks. Introduced in C++11, std::chrono allows you to perform precise time measurements, manipulate time points, and handle various units of time such as seconds, milliseconds, microseconds, and more. It is a powerful and flexible way to work with time-related operations in modern C++.

Key Concepts in std::chrono

Example 1: Measuring Elapsed Time

A common use case for std::chrono is measuring the time taken by a block of code to execute.

#include <iostream>
#include <chrono>
#include <thread>

int main() {
    using namespace std::chrono;

    // Record the start time
    auto start = high_resolution_clock::now();

    // Simulate work by sleeping for 1 second
    std::this_thread::sleep_for(seconds(1));

    // Record the end time
    auto end = high_resolution_clock::now();

    // Calculate the elapsed time
    duration<double> elapsed = end - start;

    std::cout << "Elapsed time: " << elapsed.count() << " seconds" << std::endl;

    return 0;
}

Explanation:

Example 2: Working with Durations

Durations represent time intervals. std::chrono provides predefined duration types for common time units such as seconds, milliseconds, microseconds, etc.

Copy code
#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;

    // Define a duration of 5 seconds
    seconds five_seconds(5);

    // Define a duration of 2000 milliseconds
    milliseconds two_thousand_ms(2000);

    // Convert milliseconds to seconds
    seconds converted = duration_cast<seconds>(two_thousand_ms);

    std::cout << "5 seconds = " << five_seconds.count() << " seconds" << std::endl;
    std::cout << "2000 milliseconds = " << converted.count() << " seconds" << std::endl;

    return 0;
}

Explanation:

Example 3: Working with Time Points

Time points represent specific moments in time and are usually associated with a particular clock. You can perform arithmetic operations on time points using durations.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;

    // Get the current time as a time point
    system_clock::time_point now = system_clock::now();

    // Add 1 hour to the current time
    system_clock::time_point one_hour_later = now + hours(1);

    // Convert time point to time_t to print as calendar time
    time_t now_c = system_clock::to_time_t(now);
    time_t later_c = system_clock::to_time_t(one_hour_later);

    std::cout << "Current time: " << std::ctime(&now_c);
    std::cout << "One hour later: " << std::ctime(&later_c);

    return 0;
}

Explanation:

Example 4: Using different Clocks

std::chrono provides different types of clocks, each with its specific use cases: - system_clock: Represents the system-wide real-time clock. This is the clock you'd typically use to get the current calendar time. - steady_clock: A monotonic clock that cannot be adjusted. It is guaranteed not to go backward, making it suitable for measuring intervals. - high_resolution_clock: Provides the highest possible resolution, but its actual precision depends on the underlying hardware and operating system.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;

    // Get the current time from system_clock
    auto sys_time = system_clock::now();
    std::cout << "System clock time since epoch: " << sys_time.time_since_epoch().count() << " ticks\n";

    // Get the current time from steady_clock
    auto steady_time = steady_clock::now();
    std::cout << "Steady clock time since epoch: " << steady_time.time_since_epoch().count() << " ticks\n";

    // Get the current time from high_resolution_clock
    auto high_res_time = high_resolution_clock::now();
    std::cout << "High resolution clock time since epoch: " << high_res_time.time_since_epoch().count() << " ticks\n";

    return 0;
}

Explanation:

Example 5: Measuring Execution Time

#include <iostream>
#include <chrono>

void exampleFunction() {
    for (int i = 0; i < 1000000; ++i);
}

int main() {
    auto start = std::chrono::high_resolution_clock::now();

    exampleFunction();

    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> elapsed = end - start;

    std::cout << "Execution time: " << elapsed.count() << " seconds\n";

    return 0;
}

Explanation:

Summary

std::chrono is a powerful tool in modern C++ for handling time-related tasks in a precise, flexible, and type-safe manner. Whether you're measuring execution time, scheduling tasks, or managing timeouts, std::chrono provides the tools you need.

Previous Page | Course Schedule | Course Content