Re: trouble timing optimised g++

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux