less than opposed to not equal in for loops

Started by
14 comments, last by rethan 14 years, 5 months ago
Quote:Original post by iMalc
Quote:Original post by Sneftel
In general, the kind of bugs that you WANT to have are the ones that result in obvious problems. It's the ones that are less visible that are the real time sinks. From that perspective, using != instead of < is a good thing to do, because it doesn't hide the sort of problem you posted.
Double edged sword.
You want any logic errors in your for-loop iteration or ending conditions to be obvious in a debug build, but on the other hand you may not want a runaway loop to happen for a customer, thus it may be safer to use less-than in a release build where you'd rather hide any problem.
It isn't an assertion. The point of the example (at least, as I used it) was to show how a particular coding idiom which valued generality could lead to hidden bugs, not how a specific code block could be rewritten to elucidate bugs in that particular block. The whole point was writing the loop without thinking about it, and the consequences thereof.
Advertisement
Thanks everyone, a lot has been cleared up for me.

frob, How would I go about implementing a function into a for-loop now? That's completely new to me, but I can see using a while-loop to do that instead.
--Dbproguy - My Blog - Tips, Opinions and Reviews about C++, Video Games, and Life
One thing to note is that doing greater than/less than comparisons is (in general) more expensive than checking for equality. If this code is time dependent then you may want to do that and add an assert to make sure the value is always smaller when running a debug build.
Quote:Original post by rethan
One thing to note is that doing greater than/less than comparisons is (in general) more expensive than checking for equality.


I can't think of a single example where the less-than comparison might be more expensive than the check for equality. Would you care to show us one?
If I am correct at this point then, a reason to use != would be because then your problem shows up a little quicker, and is easier to spot so you can go back and come up with an algorithm to be able to run the for loop as desired, but if you have to do that in the first place, you obviously didn't plan the loop very well.

I think I'll have to write a blog entry on all this, but keep the discussion going, a lot of what I'm reading here is interesting and every time I re-read the replies I think I learn a little bit more.
--Dbproguy - My Blog - Tips, Opinions and Reviews about C++, Video Games, and Life
Quote:Original post by alvaro
Quote:Original post by rethan
One thing to note is that doing greater than/less than comparisons is (in general) more expensive than checking for equality.


I can't think of a single example where the less-than comparison might be more expensive than the check for equality. Would you care to show us one?



Ah I did some research and you're right so I take that back. I thought the XOR instruction was faster then CMP but it's not (CMP doesn't save results which I'm guessing is where it gets its speed.), anyhoo I thought compilers would use this for != and == but that's not the case, since it looks like CMP is as fast or faster thank XOR in all cases (mem+reg, reg+imm, ...)

http://home.comcast.net/~fbui/intel_c.html#cmp
http://home.comcast.net/~fbui/intel_x.html#xor

Sorry!

This topic is closed to new replies.

Advertisement