News for hackers. Stuff that matters.
Re:Which one will execute faster, if(flag==0) or if(0==flag)? by burgerbrain on Friday January 07, 2011 @11:01PM (#2082138)
The real correct answer: if it matters, you're already doing something wrong.
Re:Which one will execute faster, if(flag==0) or if(0==flag)? by statictype on Saturday January 08, 2011 @02:57AM (#2082607)
If someone asks you 'what is 2+2' the correct answer is not 'if you have to calculate 2+2 by hand you're Doing It Wrong'.

The correct answer is '4'

Re:Which one will execute faster, if(flag==0) or if(0==flag)? by InclinedPlane on Saturday January 08, 2011 @05:55AM (#2082831)
This is not a 2+2 question.

If someone asks you "how do I dispose of a dead body without the cops knowing about it?" The correct answer is not the simplistic direct answer to that question. Sometimes the correct answer is "why are you doing that? don't do that!"

Re:Which one will execute faster, if(flag==0) or if(0==flag)? by burgerbrain on Saturday January 08, 2011 @11:48AM (#2083326)
Exactly.

Furthermore, in this case we aren't even given enough information to derive a "true" direct answer. What's the architecture? The compiler? Hell, we don't even know the language! Assuming x86, gcc, and C, which is a best case assumption (from the standpoint of easily being able to come upon an answer, and test it) we still don't quite know. What optimization level is being used?

Without answers to all of those questions, and likely more, the only possible answer is "if you need to know, you already messed up".

Re:Which one will execute faster, if(flag==0) or if(0==flag)? by statictype on Saturday January 08, 2011 @01:02PM (#2083490)
You're right that enough information wasn't provided to get a meaningful answer. I disagree though, that the response to that is 'you messed up' instead of 'give us more info'.
Re:Which one will execute faster, if(flag==0) or if(0==flag)? by statictype on Saturday January 08, 2011 @01:01PM (#2083487)
Sometimes it is. Sometimes not. Don't you think the original question was at least interesting in a pedantic way if not useful in practice?

Yes, if you're worried about 1==x vs x==1 then you're probably writing code at the wrong level, but it's still a nice thing to investigate and reason about on a purely academic level.

In those cases, skirting the question by saying "You're doing it wrong" adds nothing to the conversation except to pull in a few "Yeah" shouts from the chorus.

Re:Which one will execute faster, if(flag==0) or if(0==flag)? by InclinedPlane on Saturday January 08, 2011 @06:22PM (#2084243)
Many people have added to the discussion.

The truly correct answer is (as others have pointed out): you shouldn't have to ask. Because if it actually matters to you then you should be using profiling, timing tools, etc. that will make it trivial to figure out which one is faster.

Re:Which one will execute faster, if(flag==0) or if(0==flag)? by mynameishere on Friday January 07, 2011 @11:33PM (#2082215)
No. If it matters you have a job involving oses or control systems or game engines and shouldn't be asking on stackoverflow.
Re:Which one will execute faster, if(flag==0) or if(0==flag)? by burgerbrain on Friday January 07, 2011 @11:56PM (#2082282)
No, even then you're doing it wrong. Either: your compiler should optimize it properly and you shouldn't be worrying about it, or you shouldn't be doing it in C anyway.

If the difference between the two is actually important, then chances are your compiler is doing a myriad of other things significantly sub-optimally as well. Most likely far more sub-optimally than the difference between either of those.

Re:Which one will execute faster, if(flag==0) or if(0==flag)? by rcfox on Friday January 07, 2011 @11:37PM (#2082225)
Even though most questions are about gluing GUI libraries to databases, that doesn't mean questions about OS or game engines are disallowed.
Re:Which one will execute faster, if(flag==0) or if(0==flag)? by __david__ on Saturday January 08, 2011 @02:52AM (#2082599)
I think the implication was that if you are programming at that level then you shouldn't be asking beginner questions (instead of just testing yourself).