cpluspluscourse icon indicating copy to clipboard operation
cpluspluscourse copied to clipboard

Present C++ timing features

Open sponce opened this issue 2 years ago • 2 comments

Mainly the chrono library, including clocks, time_point and duration so that people can easily do dedicated timing of a few lines of code, basically though code like :

#include <chrono>

// other clocktypes to high_resolution_clock are steady_clock or system_clock
// see https://www.modernescpp.com/index.php/the-three-clocks for details
std::chrono::high_resolution_clock clock;

clock::time_point startTime = clock::now();
... // code to be timed
std::chrono::duration<float> ticks = clock::now() - startTime;
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
std::cout << "it took " << duration.count() << " ticks, that is " << millis.count() << " ms\n";

sponce avatar Feb 23 '23 12:02 sponce

std::chrono::high_resolution_clock clock;

Wow, I have never seen anyone create an instance of a clock :D I did not know that works!

bernhardmgruber avatar Feb 23 '23 12:02 bernhardmgruber

I changed the milestone to the summer course, because it's an advanced topic.

bernhardmgruber avatar Feb 23 '23 12:02 bernhardmgruber