Skip to content

Instantly share code, notes, and snippets.

@aliabidzaidi
Last active December 19, 2022 19:24
Show Gist options
  • Save aliabidzaidi/5de463e77ab466db261c1f0ad1706cbc to your computer and use it in GitHub Desktop.
Save aliabidzaidi/5de463e77ab466db261c1f0ad1706cbc to your computer and use it in GitHub Desktop.
Ways to Measure Execution Time
// 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;
}
@aliabidzaidi
Copy link
Author

aliabidzaidi commented Dec 19, 2022

Results 1 (Machine 1)

> g++ timeExecutionTest.cpp  && ./a.out

Chrono | Executions :- 196460000, Total Time :- 10s, Average Time :- 50.901 ns

Systime | Executions :- 289660000, Total Time :- 10s, Average Time :- 34.523 ns

TimeAndTimeh | Executions :- 2842120000, Total Time :- 10s, Average Time :- 3.519 ns

TimeAndClockGetTime | Executions :- 304920000, Total Time :- 10s, Average Time :- 32.795 ns

@aliabidzaidi
Copy link
Author

aliabidzaidi commented Dec 19, 2022

Results 2 (Machine 1)
using -O2 compiler flag

g++ -O2 timeExecutionTest.cpp  && ./a.out

Chrono | Executions :- 332380000, Total Time :- 10s, Average Time :- 30.086 ns

Systime | Executions :- 365950000, Total Time :- 10s, Average Time :- 27.326 ns

TimeAndTimeh | Executions :- 2719670000, Total Time :- 10s, Average Time :- 3.677 ns

TimeAndClockGetTime | Executions :- 344110000, Total Time :- 10s, Average Time :- 29.060 ns

@aliabidzaidi
Copy link
Author

aliabidzaidi commented Dec 19, 2022

Results 3 (Machine 2)

> g++ -O2 timeExecutionTest.cpp  && ./a.out 

Chrono | Executions :- 223010000, Total Time :- 10s, Average Time :- 44.841 ns

Systime | Executions :- 264080000, Total Time :- 10s, Average Time :- 37.867 ns

TimeAndTimeh | Executions :- 2544020000, Total Time :- 10s, Average Time :- 3.931 ns

TimeAndClockGetTime | Executions :- 252780000, Total Time :- 10s, Average Time :- 39.560 ns

@aliabidzaidi
Copy link
Author

aliabidzaidi commented Dec 19, 2022

Results 4 (Machine 3)

> g++ -O2 timeExecutionTest.cpp && ./a.out

Chrono | Executions :- 180240000, Total Time :- 10s, Average Time :- 55.482 ns

Systime | Executions :- 212790000, Total Time :- 10s, Average Time :- 46.995 ns

TimeAndTimeh | Executions :- 2670040000, Total Time :- 10s, Average Time :- 3.745 ns

TimeAndClockGetTime | Executions :- 216940000, Total Time :- 10s, Average Time :- 46.096 ns

@aliabidzaidi
Copy link
Author

aliabidzaidi commented Dec 19, 2022

Results 5 (Machine3 Docker container)

> g++ -O2 timeExecutionTest.cpp && ./a.out

Chrono | Executions :- 178760000, Total Time :- 10s, Average Time :- 55.941 ns

Systime | Executions :- 212740000, Total Time :- 10s, Average Time :- 47.006 ns

TimeAndTimeh | Executions :- 2643840000, Total Time :- 10s, Average Time :- 3.782 ns

TimeAndClockGetTime | Executions :- 218970000, Total Time :- 10s, Average Time :- 45.668 ns

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