This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: trouble timing optimised g++


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 Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]