chrono


Include file: <chrono>

The <chrono> header, introduced in C++11, provides a flexible and precise time library for measuring time intervals and working with time points. It offers a set of time utilities that are both type-safe and efficient, allowing for precise time measurements and conversions between different time units.

Key Characteristics

Key Components

Time Units (Durations)

  1. nanoseconds, microseconds, milliseconds
  2. seconds, minutes, hours

Clocks

  1. system_clock: Wall clock time from the system-wide realtime clock
  2. steady_clock: Monotonic clock that never goes backwards
  3. high_resolution_clock: Clock with the shortest tick period available

Time Points

Example 1: Measuring Execution Time

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

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

    // Simulate some work
    std::this_thread::sleep_for(std::chrono::seconds(2));

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

    std::cout << "Execution time: " << duration.count() << " milliseconds" << std::endl;

    return 0;
}

Explanation

Example 2: Working with Different Time Units

#include <iostream>
#include <chrono>

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

    auto day = 24h;
    auto half_day = 12h;
    auto lunch_break = 1h;

    auto total_work_time = day - half_day - lunch_break;

    std::cout << "Work time: " 
              << std::chrono::duration_cast<std::chrono::minutes>(total_work_time).count() 
              << " minutes" << std::endl;

    // Converting to different units
    std::cout << "Work time in hours: " 
              << std::chrono::duration<double, std::ratio<3600>>(total_work_time).count() 
              << " hours" << std::endl;

    return 0;
}

Explanation

Example 3: Using system_clock for Calendar Time

#include <iostream>
#include <chrono>
#include <ctime>

int main() {
    auto now = std::chrono::system_clock::now();
    std::time_t now_c = std::chrono::system_clock::to_time_t(now);

    std::cout << "Current time: " << std::ctime(&now_c);

    auto one_day = std::chrono::hours(24);
    auto tomorrow = now + one_day;
    std::time_t tomorrow_c = std::chrono::system_clock::to_time_t(tomorrow);

    std::cout << "Tomorrow: " << std::ctime(&tomorrow_c);

    return 0;
}

Explanation

Example 4: Using steady_clock for Benchmarking

#include <iostream>
#include <chrono>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> v(1000000);
    std::generate(v.begin(), v.end(), std::rand);

    auto start = std::chrono::steady_clock::now();
    std::sort(v.begin(), v.end());
    auto end = std::chrono::steady_clock::now();

    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
    std::cout << "Sorting took " << duration.count() << " microseconds" << std::endl;

    return 0;
}

Explanation

Additional Considerations

Summary

The <chrono> library in C++ provides a powerful and flexible set of tools for time-related operations. It offers type-safe time durations and time points, along with various clocks for different timing needs. The library's design allows for easy conversion between different time units and precise time measurements.

Key features include:

  1. Different time units from nanoseconds to hours
  2. Various clock types for different use cases (system_clock, steady_clock, high_resolution_clock)
  3. Easy-to-use time point and duration classes
  4. Arithmetic operations on durations and time points
  5. Type-safe conversions between different time units

By using <chrono>, C++ developers can write more robust and precise time-related code, whether for benchmarking, scheduling, or working with calendar times. The library's integration with other parts of the C++ Standard Library makes it a versatile tool for a wide range of time-based programming tasks.

Previous Page | Course Schedule | Course Content