Created
September 9, 2020 08:09
-
-
Save igricart/901d388b7f4129534bf2e3dbd86b1217 to your computer and use it in GitHub Desktop.
Code to check time information from system with different units
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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