Less Than Vs Less Than or Equal To - which is more efficient?

How to know the difference between Less Than and Less Than or Equal To

When dealing with integer values we all know that $x < 2 has the same number of possibilities as $x <= 1.

Occasionally there will be a semantic reason why someone should chose the less than symbol or if they should use less than or equal to, but I was wondering if in the cases when you know the values being compared are both integers and there is no inherent semantic reason why one is more appropriate which is the most efficient to use.

I decided to perform a test using two very similar bits of code, one was designed to iterate over a for loop 100 million times using less than or equals to and the other would be modified to just use less than.

Test environment:

I was running these scripts from the command line on my Macbook Pro, these are the specs;

Model Name:	        MacBook Pro
Model Identifier:	MacBookPro8,1
Processor Name:	        Intel Core i5
Processor Speed:	2.3 GHz
Number of Processors:	1
Total Number of Cores:	2
L2 Cache (per Core):	256 KB
L3 Cache:	        3 MB
Memory:	                4 GB

The PHP version is 5.3.6

The Code:

Here are the two snippets of code I ran, nothing fancy, but if you want to test my results for yourself please run this code as is without any changes at all, just to ensure consistency.

Less Than or Equal To:
{code type=PHP}
<?PHP
$start = microtime(true);
echo "Less than or equal to done 100 million times. \n";
echo "Start: $start \n";
for ($i = 0; $i <= 100000000; $i++) {
$x = 1 + $i;
}
$end = microtime(true);
echo "End: $end \n";
echo "Difference: ".($end - $start);
?>
{/code}

Less Than:
{code type=PHP}
<?PHP
$start = microtime(true);
echo "Less than done 100 million times. \n";
echo "Start: $start \n";
for ($i = 0; $i < 100000001; $i++) {
$x = 1 + $i;
}
$end = microtime(true);
echo "End: $end \n";
echo "Difference: ".($end - $start);
?>
{/code}

The Results:

Before I give my results I should state my opinion, I figured that just less than would be more efficient, as there is only one comparison to do.  I am not sure how PHP boils down those operations and how the ALU will eventually deal with them, but I would guess that less than or equals to isn't going to have it's own function, or a combination of functions.

I ran the above code several times each at 3 different times throughout the day but didn't want to display the entire table so took the average at each sampling time and used it;

100 million iterations of less than or equals to
Iteration Microtime Difference
1 6.6050539016724
2 6.5961098670959
3 6.5776059627533
Average 6.59292324384053
100 million iterations of just less than
Iteration Microtime Difference
1 6.4923448562622
2 6.4607081413269
3 6.8488190174103
Average 6.6006240049998

Conclusions:

Just comparing the averages would indicate that my assumption was wrong, but I think considering the for loop was iterating 100 million times and the fact that the overall difference between the two bits of code was 0.00770076115927 seconds the real conclusion to be drawn here is that it just doesn't matter.

Since there is seemingly no performance gain to be had in PHP when using either less than or less than or equal to then I would suggest using your own personal preference.

I guess if you were to consider the code size of the .php file, there is one character less needed for less than, but if you are looking to optimise code by removing single characters in order to reduce the size overhead, you are already dealing with some pretty damn well optimised code!

Recent posts View all

Web DevProductivity

Keeping on top of website updates

Learn what website updates are, what they entail, why they are important, and how we can help

Freelancing

Getting the most out of your agency

Here are some tips based on years of working with, for, and as an agency on how to get the most out of any work you do.