Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure CPU time and wall clock time of a program in C++

Tags:

c++

std::clock() measures the number of clock ticks within the duration of your program. In the following code, does it calculate the CPU time or wall clock time?

std::clock_t start; double duration;

start = std::clock();

/* Your algorithm here */

duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;

In another scenario, with the following code:

std::clock_t start;
double time;
start = std::clock();
time = start  / (double) CLOCKS_PER_SEC;

What will the value of time be?

like image 259
firefly Avatar asked Feb 11 '23 09:02

firefly


1 Answers

From the documentation:

std::clock time may advance faster or slower than the wall clock, depending on the execution resources given to the program by the operating system.

like image 183
Carl Norum Avatar answered Feb 13 '23 03:02

Carl Norum