Different Ways to Get Time Intervals (C++)

const clock_t begin_time = clock();
// do sth.
const clock_t end_time = clock();
double elapsed = float(end_time – begin_time)/CLOCKS_PER_SEC;
cout << "Running time: " << elapsed << " seconds.n";
LARGE_INTEGER frequency; // ticks per second
LARGE_INTEGER t1, t2; // ticks
// get ticks per second
QueryPerformanceFrequency(&frequency);
// start timer
QueryPerformanceCounter(&t1);
// do sth.
// stop timer
QueryPerformanceCounter(&t2);
// compute and print the elapsed time in millisec
double elapsed = 1000.0*(t2.QuadPart-t1.QuadPart)/frequency.QuadPart;
cout << "Running time: " << elapsed << " ms.n";
timeval t1, t2;
// start timer
gettimeofday(&t1, NULL);
// stop timer
gettimeofday(&t2, NULL);
// compute and print the elapsed time in millisec
double elapsed = (t2.tv_sec – t1.tv_sec) * 1000.0; // sec to ms
elapsed += (t2.tv_usec – t1.tv_usec) / 1000.0; // us to ms
cout << "Running time: " << elapsed << " ms.n";

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store