Solved

MSVC compiler optimisations - too extreme or just wrong?

Posted on 2006-11-05
2
209 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

757 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

23 Experts available now in Live!

Get 1:1 Help Now