public
Last active

Cross-platform, high-resolution time measurement using C++11's std::chrono

  • Download Gist
playingwithchrono.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <iostream>
#include <chrono>
#include <thread>
 
using namespace std;
using namespace chrono;
 
int main()
{
cout << "Measurement resolution: " <<
duration_cast<nanoseconds>(high_resolution_clock::duration(1)).count()
<< "ns" << endl;
 
cout << "Will now tell thread to sleep for 1ms and measure that in actual time" << endl;
 
auto start = high_resolution_clock::now();
 
this_thread::sleep_for(milliseconds(1));
 
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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.