Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Measuring Durations and Intervals | The <chrono> Library
C++ Dates and Times

bookMeasuring Durations and Intervals

Measuring how much time has passed between two events is a key task in many C++ programs. The <chrono> library provides a powerful and type-safe way to work with durations and intervals. You can capture two moments in time, called time points, and then calculate the time that elapsed between them.

measure_duration.cpp

measure_duration.cpp

copy
12345678910111213141516171819
#include <iostream> #include <chrono> int main() { // Record the start time using steady_clock std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); // Simulate some work with a loop for (int i = 0; i < 1000000; i++); // Record the end time std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); // Calculate the duration between start and end std::chrono::steady_clock::duration elapsed = end - start; std::cout << "Elapsed ticks: " << elapsed.count() << std::endl; }

When you subtract one time_point from another, you get a duration object. This duration represents the time interval between the two points. Every duration has a count() method, which returns the number of ticks (the smallest measurable time unit for that clock) between the two time points. The actual unit of these ticks depends on the clock's resolution.

Often, you want to express the duration in standard units, such as milliseconds or seconds, rather than raw ticks. The <chrono> library provides the duration_cast utility to convert durations into the units you need.

duration_to_milliseconds.cpp

duration_to_milliseconds.cpp

copy
123456789101112131415161718
#include <iostream> #include <chrono> int main() { // Simulate start and end time points auto start = std::chrono::steady_clock::now(); for (int i = 0; i < 1000000; i++); auto end = std::chrono::steady_clock::now(); // Calculate the elapsed duration auto elapsed = end - start; // Convert the duration to milliseconds auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed); std::cout << "Elapsed time: " << ms.count() << " ms" << std::endl; }

The duration_cast function is necessary when you want to convert from one duration type to another, such as from the default clock tick type to milliseconds or seconds. This explicit conversion ensures that you do not accidentally mix incompatible units, and it makes your code more readable and type-safe.

Always use duration_cast when converting between different units in <chrono>. This helps prevent subtle bugs and ensures accurate conversions.

question mark

What is the purpose of std::chrono::duration_cast in C++?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

bookMeasuring Durations and Intervals

Svep för att visa menyn

Measuring how much time has passed between two events is a key task in many C++ programs. The <chrono> library provides a powerful and type-safe way to work with durations and intervals. You can capture two moments in time, called time points, and then calculate the time that elapsed between them.

measure_duration.cpp

measure_duration.cpp

copy
12345678910111213141516171819
#include <iostream> #include <chrono> int main() { // Record the start time using steady_clock std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); // Simulate some work with a loop for (int i = 0; i < 1000000; i++); // Record the end time std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); // Calculate the duration between start and end std::chrono::steady_clock::duration elapsed = end - start; std::cout << "Elapsed ticks: " << elapsed.count() << std::endl; }

When you subtract one time_point from another, you get a duration object. This duration represents the time interval between the two points. Every duration has a count() method, which returns the number of ticks (the smallest measurable time unit for that clock) between the two time points. The actual unit of these ticks depends on the clock's resolution.

Often, you want to express the duration in standard units, such as milliseconds or seconds, rather than raw ticks. The <chrono> library provides the duration_cast utility to convert durations into the units you need.

duration_to_milliseconds.cpp

duration_to_milliseconds.cpp

copy
123456789101112131415161718
#include <iostream> #include <chrono> int main() { // Simulate start and end time points auto start = std::chrono::steady_clock::now(); for (int i = 0; i < 1000000; i++); auto end = std::chrono::steady_clock::now(); // Calculate the elapsed duration auto elapsed = end - start; // Convert the duration to milliseconds auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed); std::cout << "Elapsed time: " << ms.count() << " ms" << std::endl; }

The duration_cast function is necessary when you want to convert from one duration type to another, such as from the default clock tick type to milliseconds or seconds. This explicit conversion ensures that you do not accidentally mix incompatible units, and it makes your code more readable and type-safe.

Always use duration_cast when converting between different units in <chrono>. This helps prevent subtle bugs and ensures accurate conversions.

question mark

What is the purpose of std::chrono::duration_cast in C++?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3
some-alt