7 ms·
Which one will execute faster, if(flag==0) or if(0==flag)?
- deleted 16y ago[deleted]
- burgerbrain 16y agoThe real correct answer: if it matters, you're already doing something wrong.
- mynameishere 16y agoNo. If it matters you have a job involving oses or control systems or game engines and shouldn't be asking on stackoverflow.
- rcfox 16y agoEven though most questions are about gluing GUI libraries to databases, that doesn't mean questions about OS or game engines are disallowed.
- __david__ 16y agoI 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).
- burgerbrain 16y agoNo, 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.
- statictype 16y agoIf 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'
- InclinedPlane 16y agoThis 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!"
- burgerbrain 16y agoExactly. 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".
- statictype 16y agoYou'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'.
- statictype 16y agoSometimes 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.
- MartinCron 16y agoCode like Yoda, you must not.
- larrik 16y agoThe if(0==flag) is perfectly valid for avoiding if(flag=0) bugs. Of course, I'd use if(!flag)
- tjsnyder 16y agoHonestly, this is like arguing tabs vs spaces.
- lukeschlather 16y agoTabs vs. spaces is basically an implementation detail of the editor. The point is that the code should always look the same. This is a semantic issue that doesn't even go away in pseudocode.
- kenjackson 16y agoAny 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.
- jat850 16y agoFaster is not even the most relevant reason why one should (and would) choose 0 == flag. edit http://news.ycombinator.com/item?id=2082165 http://news.ycombinator.com/item?id=2082165 hit on it perfectly.
- redthrowaway 16y agoIt's still hard to read.
- bd_at_rivenhill 16y agoNot when you get used to it.
- redthrowaway 16y agoThat's what they said about Lisp and Perl.
- ehsanul 16y agoI can't tell whether you're just being tongue-in-cheek, or perfectly serious. I hope the former.
- redthrowaway 16y agoLittle 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 http://www.foo.be/docs/tpj/issues/vol3_2/tpj0302-0012.html No excuse.
- natep 16y agoI 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/ http://ioccc.org/ I've seen very readable perl code, although I won't use that to make sweeping generalizations about the entire language.
- michaelty 16y agoThe correct answer is your debugger.
- Luyt 16y agoThe 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.
- daleharvey 16y agoas someone else has mentioned they have been coined yoda conditions, which I love as a name.
- wnoise 16y agoIsn't it interesting that x < y, y > x, !(x >= y), and !(y >= x) all express the same thing differently (barring NaNs)? Why should one of them be considered more "inside out" than another? Why does it make a difference if one is a constant rather than a variable? Does one of "x + 2" and "2 + x" seem inside-out as compared to the other? How about "2 * x" vs "x * 2"?
- Luyt 16y agoYou're right. I think it's just what people are used to. Compare: "If two gallons is less than the amount of gas in your tank, you should fill it up." "If you have less than two gallons of gas left in your tank, you should fill it up." In everyday speech, as well in mathematics, it is usual to state the variable first (what are we're going to compare) and then the amount to which it will be compared.
- wnoise 16y agoThe second actually splits the variable across the comparison. ("you have" and "left in your tank").
- __david__ 16y agoThe first is especially awkward because you forgot to flip the < into a >. It should be, "If two gallons is greater than the amount of gas in your tank, you should fill it up."
- dedward 16y agoDepends on the language, interpreter, vm, processor, and compiler and their relevant individual optimization settings.
- deleted 16y ago[deleted]
- known 16y agodepends on number of machine instructions they generate.
- burgerbrain 16y agoNot true in the general case. On some architectures, some instructions take longer than others.
- checoivan 16y agoI 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 16y agoI'll stick with my answer to a highly relevant related question on SO: http://stackoverflow.com/questions/767821/is-else-if-faster-than-switch-case/#767877 http://stackoverflow.com/questions/767821/is-else-if-faster-...