Which one will execute faster, if(flag==0) or if(0==flag)?
stackoverflow.com
stackoverflow.com
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.
The correct answer is '4'
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!"
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".
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.
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.
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.
"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.
People often wrongly assume that you can just execute many times the thing in a loop, and compute the average, which is the wrong statistic (you want the min, not the average when measing the speed of something which has a fixed cost).
By itself, the question is pretty stupid - maybe that's what the interviewer was getting at ?
Python will throw a SyntaxError on 'if foo = 3:'.
Of course, I'd use if(!flag)
This is a semantic issue that doesn't even go away in pseudocode.
Any non-modern language should flag a warning on that. And this warning should be set to be an error by any reasonable developer.
Flash Builder's Actionscript compiler has saved me from that a couple times in memory, though, so I agree it should be a warning if you don't wrap it in extra parens. gcc also will complain.
The problem with := is that you run the risk of the Pascal trap, in that no language which uses := for assignment has ever become more popular than Pascal.
This is how you set default values for optional argument in Ruby and is very common.
if(@post = Post.find(params[:id]))
render @post
else
render 'Post not found'
endI think that
if(S_OK == QueryInterface(IID_IUnknown, &p)) {
is clearer than if(QueryInterface(IID_IUnknown, &p) == S_OK) {edit http://news.ycombinator.com/item?id=2082165 hit on it perfectly.
http://www.foo.be/docs/tpj/issues/vol3_2/tpj0302-0012.html
No excuse.
I've seen very readable perl code, although I won't use that to make sweeping generalizations about the entire language.
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.