Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Using Clocks and Time Points | The <chrono> Library
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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain the differences between system_clock and steady_clock in more detail?

What are some practical examples of when to use each clock?

How do I use these clocks in C++ code?

bookUsing Clocks and Time Points

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2
some-alt