Skip to content

Instantly share code, notes, and snippets.

@blippy
Created November 12, 2016 19:52
Show Gist options
  • Select an option

  • Save blippy/69339dcac2e88fb6d8b581d98a5b855a to your computer and use it in GitHub Desktop.

Select an option

Save blippy/69339dcac2e88fb6d8b581d98a5b855a to your computer and use it in GitHub Desktop.
Time sections of code in nanoseconds
#include <chrono>
class Timer
{
public:
Timer() { start(); };
void start() {m_start = std::chrono::high_resolution_clock::now(); };
void stop() {m_stop = std::chrono::high_resolution_clock::now(); };
double nanos() { std::chrono::duration<double> d = m_stop - m_start;
return std::chrono::duration_cast<std::chrono::nanoseconds>(d).count(); };
private:
std::chrono::time_point<std::chrono::high_resolution_clock>
m_start, m_stop;
};
void using_it()
{
Timer tim;
tim.start();
// ... some code that you want to time
tim.stop();
cout << "Elapsed: " << tim.nanos() << endl;
// more timing
tim.start();
// ... yet more code
tim.stop();
cout << "Elapsed: " << tim.nanos() << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment