Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Cross-platform, high-resolution time measurement using C++11's std::chrono
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
using namespace chrono;
int main()
cout << "Measurement resolution: " <<
<< "ns" << endl;
cout << "Will now tell thread to sleep for 1ms and measure that in actual time" << endl;
auto start = high_resolution_clock::now();
auto end = high_resolution_clock::now();
auto dur = duration_cast<nanoseconds>(end - start);
cout << "Measured time: " << dur.count() << "ns" << endl;
cout << "Delta: " <<
duration_cast<nanoseconds>(dur - milliseconds(1)).count()
<< "ns" << endl;
return 0;

Lamzin commented Sep 15, 2015

Thanks for sharing!

amorozov commented Oct 9, 2015

I would use steady_clock instead of high_resolution_clock, since it's the only one which guarantees, well, steady timer. See this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment