Solved

MSVC compiler optimisations - too extreme or just wrong?

Posted on 2006-11-05
2
210 Views
Last Modified: 2010-04-01
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
Comment
Question by:Unimatrix_001
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 17877103
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
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 17877114
Yep! Thanks for that jkr. Enjoy your points. Clearly my ignorance of compiler optimisations shows though! :S Hehehe,

Thanks again.
Uni
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now