Measure execution time
This sample is licensed under the CC0 Public Domain Dedication.
Measure the execution time of a unit of code.
Before and after the task that we wish to time (line 7 and line 9
respectively), we call the static member function
which gives us a
std::chrono::time_point representing the
point in time at which it was called. By subtracting the
time point from the
end time point on line 11, we get a
std::chrono::duration representing the
time taken between these time points and, therefore, the execution
time of our task.
We can access the value of a
std::chrono::duration by calling its
count member function. The units of the
are determined by the clock it came from, but it can be casted to
std::duration with different units with the
function (or implicitly, if the units are exactly divisible).