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++.
std::chrono
offers different types of clocks, like system clocks and steady clocks.std::chrono
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;
}
high_resolution_clock::now()
: Captures the current time as a time_point from the high-resolution clock.std::this_thread::sleep_for(seconds(1))
: Puts the current thread to sleep for 1 second.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;
}
std::chrono.
duration_cast
: Converts one duration type to another, in this case, milliseconds to seconds.count()
: Returns the numerical value of the duration.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;
}
system_clock::time_point now = system_clock::now();
: Captures the current time using the system clock.now + hours(1)
: Adds 1 hour to the current time, resulting in a new time point.std::ctime(&now_c)
: Converts the time point to a time_t
and then to a human-readable string.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;
}
system_clock::now()
: Retrieves the current system time.steady_clock::now()
: Retrieves the current time from a clock that is guaranteed not to jump or be adjusted.high_resolution_clock::now()
: Retrieves the time with the highest resolution available.#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;
}
exampleFunction()
: A simple function whose execution time is measured.high_resolution_clock::now()
: Captures the start and end time points to calculate the execution duration.duration<double>
: Represents the elapsed time in seconds, though it can be in any unit depending on the type used.std::chrono
: A comprehensive library for dealing with time, including durations, time points, and clocks.std::chrono
is widely used for performance measurement, timeout management, and working with real-world date and time.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.