Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

MSVC compiler optimisations - too extreme or just wrong?

Hi all,

======================================
      #define PI 3.14159265
======================================
      DWORD startTime;
      DWORD endTime;
      DWORD durationTime;
      long number=200000000;

      startTime=GetTickCount();

      double res=0;
      for(long arg=0;arg<number;arg++)
            res=sin(arg*PI/180);

      endTime=GetTickCount();
      durationTime=endTime-startTime;
      char buff[50];
      memset(buff, 0, 50);
      sprintf(buff, "%d", durationTime);
      MessageBox(0, buff, "Duration", 0);
======================================

That program, when running in debug mode takes approx 14,000 ms. However, if I run this in release mode, it takes 0 ms... The time improvements just seem a bit too drastic to be correct, maybe I'm doing something stupid in the code that I haven't realised?

Thanks,
Uni
0
Unimatrix_001
Asked:
Unimatrix_001
1 Solution
 
jkrCommented:
It might well be the optimizer, since the calculation result in the loop is unused. Try

double* res = new double[number];

     for(long arg=0;arg<number;arg++)
          res[arg]=sin(arg*PI/180);

delete [] res;

to rule that out.
0
 
Unimatrix_001Author Commented:
Yep! Thanks for that jkr. Enjoy your points. Clearly my ignorance of compiler optimisations shows though! :S Hehehe,

Thanks again.
Uni
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now