Friday, June 28, 2019

performance - Does writing i != 0 compare faster or slower than i > 0 for a positive integer?




Imagine I have a program that needs to check if a variable i is greater than zero. i is always positive, so saying that i > 0 is equivalent to saying i != 0.



Is there a performance difference between those two expressions and why?




I am aware that there isn't a noticable performance difference, this is more of a philosophical question.


Answer



I don't think it's measurably different, but contrary to popular wisdom, I'm going to tell you to use != rather than > or < on the grounds that the former is a more general operation, and if you were going to convert your code to C++ and use iterators instead of pointers, not all iterators would support < or > (but all of them would support !=).


No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...