How smart is the compiler?

Started by
24 comments, last by Conner McCloud 17 years, 6 months ago
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?
Construct (Free open-source game creator)
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.
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?
daerid@gmail.com
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?
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.
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.
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.

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.
Fair point, the compares are nothing compared to the cost of the actual functions themselves I guess. Thanks for the thoughts everyone.
Construct (Free open-source game creator)
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

This topic is closed to new replies.

Advertisement