Sign in to follow this  

structure if / ?: difference

This topic is 4305 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

Is there structural difference between the if/else statement and the ?: statements after compiling? For instance, if I want to do:
clear |=  ((video::BUFFER_COLOR & t) ? GL_COLOR_BUFFER_BIT : 0);
would the generated code be better than
if( video::BUFFER_COLOR & t )
	clear |= GL_COLOR_BUFFER_BIT;
when we take into account branch prediction? And while I'm here and talking about branch prediction, how much of a problem can it be, really?

Share this post


Link to post
Share on other sites
Quote:
Original post by Endar
Is there structural difference between the if/else statement and the ?: statements after compiling?

Depends on your compiler, on the assembly level. Logically, no - and thus no reason your compiler should be generating different code - both pieces of code will arrive at the same destination given the same input, and will likely result in identical code on an assembly level on a modern compiler. Feel free to try it out yourself on your compiler and look at the disassembly!!!

I would use the second statement in this case, because it makes it clear that the statement is a no-op if (video::BUFFER_COLOR & t) is false.

Any difference between the two shouldn't have any impact relating to branch prediction.

Share this post


Link to post
Share on other sites
So pretty much the only difference between the two is syntactically? I always thought that the simpler one might have actually been simpler.

Anyway, thanks.

Share this post


Link to post
Share on other sites
In your sample compilers will know that Or-ing with zero is nothing and will not create code for it (at least on release build). So both should translate to the same.

by the way, the most efficient way would be if you could equate video::BUFFER_COLOR to GL_BUFFER_BIT and create this code:
clear |= (video::BUFFER_COLOR & t);

but anyways, the difference between "if(x) Y else Z" and "X?Y:Z" is that the 2nd can be used inside expressions (like a=1+x?y:z) but this is purely high language concepts (and in some languages you can put if-statements inside expressions or even on the right side of an assignment). In Assembly language (the machine code) there is no difference between expressions and statements and both "if" and "? :" translate to same commands. Even if there were two ways to implement it in assembly with one more efficient than the other, the compiler will choose the efficient one and both would translate to same code.

Iftah.

Share this post


Link to post
Share on other sites
I remember reading that both result candidates are evaluated in ?:, which of course is not the case with if. For example in this situation both functions are called:

int x = y < z ? func1() : func2();

Share this post


Link to post
Share on other sites
Quote:
Original post by Ubik
I remember reading that both result candidates are evaluated in ?:, which of course is not the case with if. For example in this situation both functions are called:

int x = y < z ? func1() : func2();

Nope. The ternary operator does short-circuiting just like (some of) the boolean operators.

Share this post


Link to post
Share on other sites

This topic is 4305 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this