Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Using Clocks and Time Points | The <chrono> Library
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C++ Dates and Times

bookUsing Clocks and Time Points

Understanding how to work with clocks and time points is essential when dealing with time in C++. The chrono library provides several types of clocks, each suited for different timing needs. The two most commonly used clocks are std::chrono::system_clock and std::chrono::steady_clock. Each of these clocks has its own characteristics and typical use cases, which you will explore here.

main.cpp

main.cpp

copy
123456789
#include <iostream> #include <chrono> int main() { // Get the current time_point from system_clock auto now = std::chrono::system_clock::now(); std::cout << "Current system_clock time_point: " << now.time_since_epoch().count() << std::endl; }

The std::chrono::system_clock is designed to represent the current time according to the system's wall clock. This means it reflects the real-world date and time as set on your computer. You typically use system_clock when you want to record timestamps, log events, or display the current date and time to users.

main.cpp

main.cpp

copy
123456789
#include <iostream> #include <chrono> int main() { // Get the current time_point from steady_clock auto now = std::chrono::steady_clock::now(); std::cout << "Current steady_clock time_point: " << now.time_since_epoch().count() << std::endl; }

Unlike system_clock, the std::chrono::steady_clock is designed for measuring time intervals. It is guaranteed never to go backwards, even if the system time is adjusted. This makes it ideal for timing operations, benchmarking, or any situation where you need to measure the duration between two points in time without being affected by changes to the system clock.

Note
Note

Use steady_clock for measuring durations, such as benchmarking code or timing events, because it is not affected by changes to the system clock. Use system_clock when you need to get the current date and time, such as for logging or displaying timestamps.

question mark

When should you use std::chrono::steady_clock instead of std::chrono::system_clock?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookUsing Clocks and Time Points

Scorri per mostrare il menu

Understanding how to work with clocks and time points is essential when dealing with time in C++. The chrono library provides several types of clocks, each suited for different timing needs. The two most commonly used clocks are std::chrono::system_clock and std::chrono::steady_clock. Each of these clocks has its own characteristics and typical use cases, which you will explore here.

main.cpp

main.cpp

copy
123456789
#include <iostream> #include <chrono> int main() { // Get the current time_point from system_clock auto now = std::chrono::system_clock::now(); std::cout << "Current system_clock time_point: " << now.time_since_epoch().count() << std::endl; }

The std::chrono::system_clock is designed to represent the current time according to the system's wall clock. This means it reflects the real-world date and time as set on your computer. You typically use system_clock when you want to record timestamps, log events, or display the current date and time to users.

main.cpp

main.cpp

copy
123456789
#include <iostream> #include <chrono> int main() { // Get the current time_point from steady_clock auto now = std::chrono::steady_clock::now(); std::cout << "Current steady_clock time_point: " << now.time_since_epoch().count() << std::endl; }

Unlike system_clock, the std::chrono::steady_clock is designed for measuring time intervals. It is guaranteed never to go backwards, even if the system time is adjusted. This makes it ideal for timing operations, benchmarking, or any situation where you need to measure the duration between two points in time without being affected by changes to the system clock.

Note
Note

Use steady_clock for measuring durations, such as benchmarking code or timing events, because it is not affected by changes to the system clock. Use system_clock when you need to get the current date and time, such as for logging or displaying timestamps.

question mark

When should you use std::chrono::steady_clock instead of std::chrono::system_clock?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2
some-alt