Skip to content

Instantly share code, notes, and snippets.

@igricart
Created September 9, 2020 08:09
Show Gist options
  • Save igricart/901d388b7f4129534bf2e3dbd86b1217 to your computer and use it in GitHub Desktop.
Save igricart/901d388b7f4129534bf2e3dbd86b1217 to your computer and use it in GitHub Desktop.
Code to check time information from system with different units
#include <iostream>
#include <chrono>
#include <unistd.h>
using namespace std;
// main function to measure elapsed time of a C++ program
// using chrono library
int main()
{
auto start = chrono::steady_clock::now();
// do some stuff here
sleep(3);
auto end = chrono::steady_clock::now();
cout << "Elapsed time in nanoseconds : "
<< chrono::duration_cast<chrono::nanoseconds>(end - start).count()
<< " ns" << endl;
cout << "Elapsed time in microseconds : "
<< chrono::duration_cast<chrono::microseconds>(end - start).count()
<< " µs" << endl;
cout << "Elapsed time in milliseconds : "
<< chrono::duration_cast<chrono::milliseconds>(end - start).count()
<< " ms" << endl;
cout << "Elapsed time in seconds : "
<< chrono::duration_cast<chrono::seconds>(end - start).count()
<< " sec";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment