Last active
December 19, 2022 19:24
-
-
Save aliabidzaidi/5de463e77ab466db261c1f0ad1706cbc to your computer and use it in GitHub Desktop.
Ways to Measure Execution Time
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
// https://levelup.gitconnected.com/8-ways-to-measure-execution-time-in-c-c-48634458d0f9 | |
// Ways to Measure exeuction time in c++ | |
// 1. Using the ‘time’ Linux command (Run program with "time ./program") | |
// 2. Using <chrono> | |
// 3. With <sys/time.h> and gettimeofday() | |
// 4. With <time.h> and time() | |
// 5. Using <time.h> and clock() | |
// 6. With <time.h> and clock_gettime() | |
// 7. With <sysinfoapi.h> and GetTickCount64() (For windows) | |
// 8. With <processthreadsapi.h> and GetProcessTimes() (For windows) | |
#include <chrono> | |
#include <iostream> | |
#include <iomanip> | |
#include <sys/time.h> | |
#include <time.h> | |
using namespace std; | |
using namespace std::chrono; | |
const int iterations = 10000; | |
const int testDuration = 10; | |
time_t currentTime() | |
{ | |
return system_clock::to_time_t(system_clock::now()); | |
} | |
// Wall time. (flags for nano, micro, milli, seconds etc) | |
void Chrono() | |
{ | |
cout << endl; | |
auto startTime = currentTime(); | |
auto timeTaken = 0; | |
long executions = 0; | |
do | |
{ | |
for (auto i = 0; i < iterations; i++) | |
{ | |
auto start = std::chrono::high_resolution_clock::now(); | |
auto end = std::chrono::high_resolution_clock::now(); | |
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start); | |
// other options chrono::hours, chrono::minutes, chrono::seconds, chrono::milliseconds, or chrono::microseconds | |
} | |
executions += iterations; | |
timeTaken = currentTime() - startTime; | |
} while (timeTaken < testDuration); | |
auto avgTimeTaken = ((double)(timeTaken * 1e9) / executions); | |
cout | |
<< "Chrono | Executions :- " << executions | |
<< ", Total Time :- " << timeTaken << "s" | |
<< ", Average Time :- " << fixed << setprecision(3) << avgTimeTaken << " ns" | |
<< endl; | |
} | |
// Wall time, max micro seconds | |
void Systime() | |
{ | |
cout << endl; | |
auto startTime = currentTime(); | |
auto timeTaken = 0; | |
long executions = 0; | |
do | |
{ | |
for (auto i = 0; i < iterations; i++) | |
{ | |
struct timeval begin, end; | |
gettimeofday(&begin, 0); | |
gettimeofday(&end, 0); | |
long seconds = end.tv_sec - begin.tv_sec; | |
long microseconds = end.tv_usec - begin.tv_usec; | |
double elapsed = seconds + microseconds * 1e-6; | |
} | |
executions += iterations; | |
timeTaken = currentTime() - startTime; | |
} while (timeTaken < testDuration); | |
auto avgTimeTaken = ((double)(timeTaken * 1e9) / executions); | |
cout | |
<< "Systime | Executions :- " << executions | |
<< ", Total Time :- " << timeTaken << "s" | |
<< ", Average Time :- " << fixed << setprecision(3) << avgTimeTaken << " ns" | |
<< endl; | |
} | |
// Wall time but only seconds | |
void TimeAndTimeh() | |
{ | |
cout << endl; | |
auto startTime = currentTime(); | |
auto timeTaken = 0; | |
long executions = 0; | |
do | |
{ | |
for (auto i = 0; i < iterations; i++) | |
{ | |
time_t begin, end; | |
time(&begin); | |
time(&end); | |
time_t elapsed = end - begin; | |
} | |
executions += iterations; | |
timeTaken = currentTime() - startTime; | |
} while (timeTaken < testDuration); | |
auto avgTimeTaken = ((double)(timeTaken * 1e9) / executions); | |
cout | |
<< "TimeAndTimeh | Executions :- " << executions | |
<< ", Total Time :- " << timeTaken << "s" | |
<< ", Average Time :- " << fixed << setprecision(3) << avgTimeTaken << " ns" | |
<< endl; | |
} | |
// CPU time as clock ticks on Linux | |
void TimeAndClock() | |
{ | |
cout << endl; | |
auto startTime = currentTime(); | |
auto timeTaken = 0; | |
long executions = 0; | |
do | |
{ | |
for (auto i = 0; i < iterations; i++) | |
{ | |
// Start measuring time | |
clock_t start = clock(); | |
// Stop measuring time and calculate the elapsed time | |
clock_t end = clock(); | |
double elapsed = double(end - start) / CLOCKS_PER_SEC; | |
} | |
executions += iterations; | |
timeTaken = currentTime() - startTime; | |
} while (timeTaken < testDuration); | |
auto avgTimeTaken = ((double)(timeTaken * 1e9) / executions); | |
cout | |
<< "TimeAndClock | Executions :- " << executions | |
<< ", Total Time :- " << timeTaken << "s" | |
<< ", Average Time :- " << fixed << setprecision(3) << avgTimeTaken << " ns" | |
<< endl; | |
} | |
// CPU time and wall time | |
void TimeAndClockGetTime() | |
{ | |
cout << endl; | |
auto startTime = currentTime(); | |
auto timeTaken = 0; | |
long executions = 0; | |
do | |
{ | |
for (auto i = 0; i < iterations; i++) | |
{ | |
// Start measuring time | |
struct timespec begin, end; | |
clock_gettime(CLOCK_REALTIME, &begin); | |
// Stop measuring time and calculate the elapsed time | |
clock_gettime(CLOCK_REALTIME, &end); | |
long seconds = end.tv_sec - begin.tv_sec; | |
long nanoseconds = end.tv_nsec - begin.tv_nsec; | |
double elapsed = seconds + nanoseconds * 1e-9; | |
} | |
executions += iterations; | |
timeTaken = currentTime() - startTime; | |
} while (timeTaken < testDuration); | |
auto avgTimeTaken = ((double)(timeTaken * 1e9) / executions); | |
cout | |
<< "TimeAndClockGetTime | Executions :- " << executions | |
<< ", Total Time :- " << timeTaken << "s" | |
<< ", Average Time :- " << fixed << setprecision(3) << avgTimeTaken << " ns" | |
<< endl; | |
} | |
int main(int argc, char **argv) | |
{ | |
Chrono(); | |
Systime(); | |
TimeAndTimeh(); | |
TimeAndClockGetTime(); | |
cout << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results 5 (Machine3 Docker container)