Quantcast
Channel: Measuring the runtime of a C++ code? - Stack Overflow
Viewing all articles
Browse latest Browse all 8

Answer by vitaut for Measuring the runtime of a C++ code?

$
0
0

Use std::chrono::steady_clock and not std::chrono::system_clock for measuring run time in C++11. The reason is (quoting system_clock's documentation):

on most systems, the system time can be adjusted at any moment

while steady_clock is monotonic and is better suited for measuring intervals:

Class std::chrono::steady_clock represents a monotonic clock. The timepoints of this clock cannot decrease as physical time moves forward.This clock is not related to wall clock time, and is best suitable formeasuring intervals.

Here's an example:

auto start = std::chrono::steady_clock::now();// do somethingauto finish = std::chrono::steady_clock::now();double elapsed_seconds = std::chrono::duration_cast<  std::chrono::duration<double>>(finish - start).count();

A small practical tip: if you are measuring run time and want to report seconds std::chrono::duration_cast<std::chrono::seconds> is rarely what you need because it gives you whole number of seconds. To get the time in seconds as a double use the example above.


Viewing all articles
Browse latest Browse all 8

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>