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

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

$
0
0

This is the code I use:

  const auto start = std::chrono::steady_clock::now();  // Your code here.  const auto end = std::chrono::steady_clock::now();  std::chrono::duration<double> elapsed = end - start;  std::cout << "Time in seconds: "<< elapsed.count() << '\n';

You don't want to use std::chrono::system_clock because it is not monotonic! If the user changes the time in the middle of your code your result will be wrong - it might even be negative. std::chrono::high_resolution_clock might be implemented using std::chrono::system_clock so I wouldn't recommend that either.

This code also avoids ugly casts.


Viewing all articles
Browse latest Browse all 8

Trending Articles



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