

Which one will execute faster, if(flag==0) or if(0==flag)? - solipsist
http://stackoverflow.com/questions/4624536/which-one-will-execute-faster-ifflag-0-or-if0-flag

======
burgerbrain
The real correct answer: if it matters, you're already doing something wrong.

~~~
mynameishere
No. If it matters you have a job involving oses or control systems or game
engines and shouldn't be asking on stackoverflow.

~~~
rcfox
Even though most questions are about gluing GUI libraries to databases, that
doesn't mean questions about OS or game engines are disallowed.

~~~
__david__
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).

------
Luyt
The first thing that comes to my mind when someone asks such questions (quite
often seen in IRC channels), is: _"Well, why don't you time both versions and
actually measure which is faster?"_

Also, premature optimisation is the root of all evil ;-)

There is also an aesthetical side to it. Personally I find constructs like 'if
(6.28 < theta) {...}' unpleasant to read. They look like thoughts turned
inside out.

~~~
xyzzyz
I use them conventionally -- it helps me avoid bugs like 'if(foo = 3)' instead
of 'if(foo == 3)'. 'if(3 = foo)' just will not compile. The 'if(6.28 < theta)'
is just a logical consequence.

~~~
Luyt
My C/C++ compiler will give a warning on 'if (foo=3)'.

Python will throw a SyntaxError on 'if foo = 3:'.

------
dedward
Depends on the language, interpreter, vm, processor, and compiler and their
relevant individual optimization settings.

------
MartinCron
Code like Yoda, you must not.

~~~
larrik
The if(0==flag) is perfectly valid for avoiding if(flag=0) bugs.

Of course, I'd use if(!flag)

~~~
kenjackson
Any modern should not allow 'if(flag=0)'.

Any non-modern language should flag a warning on that. And this warning should
be set to be an error by any reasonable developer.

~~~
rcfox
What about while? while(flag = 0) looks like a similar error, but while(foo =
read()) is a common idiom.

~~~
brownleej
I think some compilers give a warning on while(foo = read()) but not on
while((foo = read())).

~~~
CamperBob
That would be a broken compiler.

~~~
tbrownaw
No, I'm pretty sure that's _intentional_ specifically to permit that idiom.

~~~
__david__
It's usually a warning that you have to turn on explicitly, or one that you
can at least turn off (thank heavens).

------
michaelty
The correct answer is your debugger.

------
jat850
Faster is not even the most relevant reason why one should (and would) choose
0 == flag.

 _edit_ <http://news.ycombinator.com/item?id=2082165> hit on it perfectly.

~~~
redthrowaway
It's still hard to read.

~~~
bd_at_rivenhill
Not when you get used to it.

~~~
redthrowaway
That's what they said about Lisp and Perl.

~~~
ehsanul
I can't tell whether you're just being tongue-in-cheek, or perfectly serious.
I hope the former.

~~~
redthrowaway
Little of both, really. I actually think Lisp is beautiful, although it can be
difficult to read. As for Perl...

<http://www.foo.be/docs/tpj/issues/vol3_2/tpj0302-0012.html>

No excuse.

~~~
natep
I don't think you're supposed to be able to get used to obfuscated anything.
By that metric, C is equally unreadable: <http://ioccc.org/>

I've seen very readable perl code, although I won't use that to make sweeping
generalizations about the entire language.

~~~
redthrowaway
To be fair, I never said C was pretty. I have no real problem with Perl aside
from its overabundance of punctuation.

------
checoivan
I would say pretty much they'll be the same, but as always it depends on what
get the compiler ends up generating. Likely it'll be something like:

mov ax,0x####

mov bx,0000

cmp ax, bx

je equal

I say same cause the order of the data moves into the registers shouldn't
matter in terms of perf. I 'guess' If you happen to see a minimal difference
it would be because an interrupt happened in the middle of the execution,or
there's some funky compiler optimization for one of the cases or if...

ALL GLORY TO THE HYPNOTOAD.

------
InclinedPlane
I'll stick with my answer to a highly relevant related question on SO:
[http://stackoverflow.com/questions/767821/is-else-if-
faster-...](http://stackoverflow.com/questions/767821/is-else-if-faster-than-
switch-case/#767877)

------
known
depends on number of machine instructions they generate.

~~~
burgerbrain
Not true in the general case. On some architectures, some instructions take
longer than others.

