<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.
nanoseconds
, microseconds
, milliseconds
seconds
, minutes
, hours
system_clock
: Wall clock time from the system-wide realtime clocksteady_clock
: Monotonic clock that never goes backwardshigh_resolution_clock
: Clock with the shortest tick period availabletime_point
: Represents a point in 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;
}
high_resolution_clock
to measure time with high precisionduration_cast
to convert between 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;
}
#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;
}
system_clock
to get the current timechrono
time points and C-style time#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;
}
steady_clock
for precise timing of an operationsteady_clock
is preferred for measuring intervals as it's guaranteed to be monotonicsystem_clock
can be adjusted and may jump forwards or backwardsThe <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.
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.