Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MSVC compiler optimisations - too extreme or just wrong?

Posted on 2006-11-05
2
Medium Priority
?
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

704 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