Take the 2-minute tour ×

A very minor saving either way but it prompted a mini-debate in the office and I wanted to get other feedback on it.

Assuming integer values for $x which is more efficient;

($x >= 3) 


($x > 2)


I have rolled back a wording change because one of the factors we discussed is that some believed >= to be easier to immediately read and felt that for int values > 2 was pointless because you meant >= 3. This does not make the script faster but potentially more efficient in terms of maintaining it. (in before people explain how tiny a point this is)

share|improve this question
There will be no performance difference. But the meaning is different for floating-point numbers! –  Konrad Rudolph Nov 3 '10 at 14:48
But what about 2.00000000...1 - 2.999999...? –  Harmen Nov 3 '10 at 14:50
I'm at a loss of words of how infinitely mindbogglingly irrelevant this question is. When this question comes up, you're already waaaay beyond xkcd.org/554. I nearly wish god killed a kitten every time such a question appears, maybe it would stop people from wasting everyone's time with them. –  delnan Nov 3 '10 at 14:56
@delnan It is a coding question with an absolute answer. No one is saying this is an important question but it is a question. –  Toby Nov 3 '10 at 15:03
@Toby: Agreed. And even if it may end up being a silly question, good and useful information can still be posted about it (such as Konrad's comment). That is, after all, what SO is all about. Share the information, let history decide if it's relevant. –  David Nov 3 '10 at 15:08
show 4 more comments

5 Answers

up vote 12 down vote accepted

On my test machine (PHP 5.3.3) I ran 5 runs of 10 million iterations of each and averaged the results. "$x > 2" took 5.7357 seconds while "$x >= 3" took 5.8654. Note this excludes compilation time because compilation occurred only once for these iterations. So "$x > 2" is faster, but barely. I don't understand why, but there you go.

share|improve this answer
+1 - Test while others debate :) –  David Nov 3 '10 at 15:13
That doesn't say anything. Is the result statistically significant? –  George Nov 3 '10 at 15:58
With a difference that small (0.13 seconds for 10 million iterations), I'd guess that it isn't significant, certainly not in general use. –  ssube Nov 3 '10 at 19:38
I'm not sure this test proves anything — not even an insignificant difference. With a variation that tiny, it could just be some other process taking up a few milliseconds of time in the middle of the >= test. We'd have something if, with a billion iterations, it takes 10 seconds longer. –  Chuck Nov 3 '10 at 19:52
add comment

I can't imagine the operations themselves will differ at all in execution time. However, there will be an absolutely miniscule increased time for the first one because the lexer/parser will have to deal with one whole extra character.

Definitely not worth worrying about

share|improve this answer
Agreed, but you know how nerds like to bicker. –  Toby Nov 3 '10 at 14:51
add comment

Empirical evidence will tell you. Run your own benchmarks instead of running to the Internet to have other people give you the answer.

share|improve this answer
Any tests I run will be very small samples and with something as minute as this I thought asking the internet at large was the best idea. –  Toby Nov 3 '10 at 16:42
So instead of running very small samples, run very large samples. They will give you the answer you want. –  Andy Lester Nov 3 '10 at 19:29
Sorry what I meant was that will show me for the machine I am currently running the benchmarks on. I have since ran some tests and agree with @alex (which is why I marked his answer as the correct one) –  Toby Nov 3 '10 at 19:33
add comment

A very minor saving is an understatement. Anyway I believe none is faster - there are assembly instructions for both

share|improve this answer
Yes... although it's not like PHP uses those. –  delnan Nov 3 '10 at 14:52
add comment

PHP will, in your case, swap the operand sand then run a smaller or smaller-or-equal comparison. If you look at the implementation of is_smaller_function and is_smaller_or_equal_functionion on http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_operators.c#1714

There you can see that th only difference is the operator in the last line

ZVAL_BOOL(result, (Z_LVAL_P(result) < 0));


ZVAL_BOOL(result, (Z_LVAL_P(result) <= 0));

So the answer depends on the C compiler, whether it can optimize either of those operations in different ways and the CPU.

There ight be a difference, but dependig on it might not be that useful and you should use the way which is clearer to read.

share|improve this answer
add comment

Your Answer


