trouble timing optimised g++

Marc Glisse marc.glisse+gcc@normalesup.org
Mon May 24 21:00:00 GMT 2010


On Sun, 23 May 2010, me wrote:

> if I compile with "g++ speed_test.cpp"
> I get some ticks out of the following
>
> #include "iostream"
> #include <time.h>
> using namespace std;
> int main(){
>   float x = 1;
>   float y = 1.000001;
>
>   clock_t ticks_at_start = clock();
>   for(long i=0;i<100000000;i++){
>      x = x * y;
>   }
>   clock_t ticks_taken = clock() - ticks_at_start;
>   cout << "took " << ticks_taken << " ticks" << endl;
>   //ticks_taken was 640,000 & macro "CLOCKS_PER_SEC"=1000000
>   //so g++ (without -O2 & -03) doing this in .64 seconds
>   return 0;
> }
>
> Whereas, if I say "g++ -O2 speed_test.cpp"..
> I don't ??
> Any advice much appreciated

You mean it becomes instantaneous with -O2? That's normal. The compiler 
detects that the loop only modifies x, which is unused afterwards, so the 
loop is useless and the compiler removes it. Try printing x after the 
loop.



More information about the Gcc-help mailing list