Jump to content
  • Advertisement
Sign in to follow this  
AshleysBrain

How smart is the compiler?

This topic is 4372 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using Visual Studio 2005 and I was writing a while loop like this (C++):
const bool x = GetSomeBool();

while (running) {
	if (x) FuncA();
	FuncB();
}

Since 'x' is const and it can know the result of the if every time, will the compiler perform the following optimisation?:
if (x) while(running) { FuncA(); FuncB(); }
else while(running) { FuncB(); }

Is it smart enough to do that? The performance of this while is important, and I actually have 4 or 5 ifs. Writing out every combination is lengthy in the source code, but would the compiler do this for me?

Share this post


Link to post
Share on other sites
Advertisement
I don't think it's allowed to. What if, say, FuncA() modified x?
In practice, these things shouldn't happen an the compiler is smart enough to find out such things through simple static analysis AND act on it. It's just that a sadistic program could break this kind of behaviour.

Share this post


Link to post
Share on other sites
If x is a const bool, then possibly. Or if x is a #define.

However, what's wrong with hand coding it yourself the way you want?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
the answer to this certainly varies with the compilers you are using:so, why don't you simply write a small testcase and check your compiler's output for the testcase?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
To really be sure of what the optimized is doing for you you should instruct the compiler to generate an assembly output using the /Fa command-line switch. Make sure, however, to do that in release mode.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
I don't think it's allowed to. What if, say, FuncA() modified x?
In practice, these things shouldn't happen an the compiler is smart enough to find out such things through simple static analysis AND act on it. It's just that a sadistic program could break this kind of behaviour.


But if x is a const bool, then any program that modifies it is malformed. The only question should be whether or not the compiler knows that x is const.

Pointer to const would be a problem, though... :/

Quote:
Origianl post by daerid
If x is a const bool, then possibly. Or if x is a #define.


Actually, in this case, a #define would not allow this optimization since it's a function call which, so far as we can tell, might not always return the same value.

Share this post


Link to post
Share on other sites
Quote:
Original post by Way Walker
But if x is a const bool, then any program that modifies it is malformed.


Oops, I overlooked the const. The compiler can make this optimization safely.

Share this post


Link to post
Share on other sites
You may just want to know out of curiousity, but if you are wondering this out of serious concern for your program's speed, I wouldn't worry about it. Write the code how you think is best (which can incorporate some friendliness with the compiler, but nothing too particular). Even if it doesn't perform this optimization, the cost of a couple thousand compares is nothing to what you could probably save elsewhere.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by ToohrVyk
Quote:
Original post by Way Walker
But if x is a const bool, then any program that modifies it is malformed.


Oops, I overlooked the const. The compiler can make this optimization safely.


OMFG!!!! Everyone look! He made a mistake! He is human!!!

Hehe, I joke. You know we love you.... :P

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!