Question:
Execution time C++ program?
josh h
2011-10-04 05:37:07 UTC
I used this code to determine the execution time:
#include
#include
using namespace std;

int main()
{
clock_t start, end;
double diff;
start = clock();
for (double i = 0; i < 500; i++)
{
}
end = clock();
diff = (double(start-end)/CLOCKS_PER_SEC);
cout << diff;
return 0;
}

However, I always got a result of 0! I tried using long double and still got a result of 0, does anyone know what I can do to get time when its smaller than 0?
Four answers:
Redsatori
2011-10-04 06:02:27 UTC
Your for loop is being optimized out by the compiler automatically, since it serves no purpose. If you put even a single statement in there, it would loop through.
Alexis Martial
2011-10-04 13:08:03 UTC
Your code is OK, except that I would compute (end-start) instead of (start-end) to get positive value.



The issue is you are trying to measure the time for your processor to basically do 500 times an addition and a test.With today processors that include floating point units and run at let's say 2.4GHz, that would mean a few micro seconds.



Let's be more precise, here is the disassembly of this code on Windows processor, non optimized mode:



for (double i = 0; i < 500000000; i++)

010013C0 fldz

010013C2 fstp qword ptr [i]

010013C5 jmp wmain+43h (10013D3h)

010013C7 fld qword ptr [i]

010013CA fadd qword ptr [__real@3ff0000000000000 (1005850h)]

010013D0 fstp qword ptr [i]

010013D3 fld qword ptr [__real@415312d000000000 (1005840h)]

010013D9 fcomp qword ptr [i]

010013DC fnstsw ax

010013DE test ah,41h

010013E1 jne wmain+55h (10013E5h)

{

}

010013E3 jmp wmain+37h (10013C7h)



9 instructions, everything in the cache so let's say 9 cycles [actually it's both more and less due to the pipelining architecture of current processors].



My processor is running at 3.3GHz

thus duration = 500 * 9 / 3.3e9

= 1.363e-6 thus around 1.4 microseconds, no way to measure such timing.



To confirm my estimate I run this code using 500 000 000 i.e. 1 million time more so should takearound 1.4s



And I got 1.537s so quite close to my estimate ;-)



Here is the modified code for reference:

clock_t start, end;

double diff;

start = clock();

for (double i = 0; i < 500000000; i++)

{

}

end = clock();

diff = (double(end-start)/CLOCKS_PER_SEC);

cout << diff;
green meklar
2011-10-04 15:59:37 UTC
Check the macro CLOCKS_PER_SEC defined by time.h and see what its value is. On my machine, its value is 1000, indicating that the value returned by clock will increment by 1 every millisecond. On modern computers, executing a loop with length 500 takes far less than one millisecond, so almost all the time the loop will end during the same millisecond that it began and you will accordingly get an answer of 0.



It is possible to get time in shorter increments, but you have to jump through a few hoops to do it, and it depends on your operating system. Check out this page:

http://stackoverflow.com/questions/275004/c-timer-function-to-provide-time-in-nano-seconds
Cubbi
2011-10-04 14:03:03 UTC
No self-respecting compiler would let that loop in



Here's the assembly output from GCC 4.6.1



     call clock

     movq %rax, %rbx

     call clock

     subq %rax, %rbx



Here's the assembly output from CLang++ 2.9



     callq clock

     movq %rax, %rbx

     callq clock

     subq %rax, %rbx



Here's the output from Intel C++ 11.1



     call clock

     movq %rax, %r14

     call clock

     subq %rax, %r14



C++ may be much too high-level for your goals, consider timing inline assembly code instead (oh, and as others correctly pointed out, even if your loop *is* compiled, it's much too short to be measurable with clock())


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...