Right now this page has a huge amount of attention by the international website programming community. Because of this I've had many many different opinions emailed to me about what people think about these results and how they were found.
Keep sending them in.
This project has been a long time dream for me and I'd love to keep on learning about what I'm doing right / wrong and how we should approach this topic. If you have any information / opinions that you would like to share (
especially about the larger bottleneck problems), please contact me via
my portfolio.
Chris Vincent
PHPBench.com was constructed as a way to open people's eyes to the fact that not every PHP code snippet will run at the same speed. You may be surprised at the results that this page generates, but that is ok. This page was also created so that you would be able to find discovery in these statistics and then maybe re-run these tests in your own server environment to play around with this idea yourself, by using the code examples (these code examples are automatically generated and as the code in my .php files change, so do they).
NOTE: You must keep in mind to refresh this page a few times to "catch" the right result. The numbers change sometimes drastically during each refresh. I assume that this is because of PHP's memory garbage collector that drops in randomly and also other processes that run on this machine have an influence.
NOTE: The microtime() method for testing out these tests has only been utilised for simplicities sake. If anything more advanced was used, those whom are just starting out on their PHP journey would have difficulties understanding.
Counting LoopsFor vs. While
Is there an actual difference between counting up between the for loop and the while loop?
+ 127 %
for($i = 0; $i < 1000000; ++$i);
Total time: 121494 µsview code
+ 100 %
$i = 0; while($i < 1000000) ++$i;
Total time: 96000 µsview code
Conclusion:
Well there you have it, the while loop 90% of the time is indeed slightly faster
Using the =&-ref-operator$obj = new SomeClass() vs. $obj =& new SomeClass()
Is a good idea to use the =&-ref-operator when creating a new object? Call 1'000x
+ 100 %
$obj = new SomeClass();
Total time: 679 µsview code
+ 105 %
$obj =& new SomeClass();
Total time: 711 µsview code
Conclusion:
There seams to be no difference in performance.
String Outputecho vs. print
Is a there a difference between what option you use to output your content?. Called within Output Buffering 1'000x
+ 174 %
echo 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'
Total time: 233 µsview code
+ 146 %
print 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'
Total time: 196 µsview code
+ 233 %
echo 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa'
Total time: 312 µsview code
+ 434 %
echo 'aaaaaaa','aaaaaaa','aaaaaaa','aaaaaaa'
Total time: 581 µsview code
+ 231 %
print 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa'
Total time: 309 µsview code
+ 660 %
$a = 'aaaaaaa';
echo 'aaaaaaa'.$a.'aaaaaaa'.$a
Total time: 884 µsview code
+ 580 %
$a = 'aaaaaaa';
echo 'aaaaaaa',$a,'aaaaaaa',$a
Total time: 777 µsview code
+ 638 %
$a = 'aaaaaaa';
print 'aaaaaaa'.$a.'aaaaaaa'.$a
Total time: 855 µsview code
+ 663 %
$a = 'aaaaaaa';
echo $a.$a.$a.$a
Total time: 889 µsview code
+ 563 %
$a = 'aaaaaaa';
echo $a,$a,$a,$a
Total time: 754 µsview code
+ 738 %
$a = 'aaaaaaa';
print $a,$a,$a,$a
Total time: 989 µsview code
Conclusion:
In reality the echo and print functions serve the exact purpose and therefore in the backend the exact same code applies. The one small thing to notice is that when using a comma to separate items whilst using the echo function, items run slightly faster.
Variable Type CheckingisSet() vs. empty() vs. is_array()
What is the performance of isSet() and empty(). Call 2'000x
+ 199 %
isSet() with var that was set
Total time: 531 µsview code
+ 203 %
empty() with var that was set
Total time: 543 µsview code
+ 100 %
isSet() with var that was *not* set
Total time: 267 µsview code
+ 112 %
empty() with var that was *not* set
Total time: 300 µsview code
+ 134 %
isSet() with array-var that was set
Total time: 359 µsview code
+ 166 %
empty() with array-var that was set
Total time: 442 µsview code
+ 105 %
isSet() with array-var that was *not* set
Total time: 281 µsview code
+ 129 %
empty() with array-var that was *not* set
Total time: 345 µsview code
+ 337 %
is_array() of an array
Total time: 900 µsview code
+ 384 %
is_array() of a string
Total time: 1025 µsview code
+ 1249 %
is_array() of a non set value
Total time: 3334 µsview code
+ 1307 %
isSet() AND is_array() of a non set value
Total time: 3490 µsview code
Conclusion:
isSet() and empty() are identical. So alway check if val is set at all befor using type-checking. E.g. if (isSet($foo) AND is_array($foo))
Modify Loop:
foreach() vs. for vs. while(list() = each())
What would happen if we alter the reading loop test to test the results of a loop created to simply alter the data in each of the values in the array?
Given again is a Hash array with 100 elements, 24byte key and 10k data per entry.
+ 936 %
foreach($aHash as $key=>$val) $aHash[$key] .= "a";
Total time: 1067 µsview code
+ 186 %
while(list($key) = each($aHash)) $aHash[$key] .= "a";
Total time: 212 µsview code
+ 100 %
$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= "a";
Total time: 114 µsview code
Conclusion:
Proof in this example shows how functionally murderous the foreach() loop can be.
Control Structuresswitch/case/default vs. if/elseif/else
Is a there a difference between switch and if structures?. Call 1'000x
+ 142 %
if and elseif (using ==)
Total time: 452 µsview code
+ 146 %
if, elseif and else (using ==)
Total time: 466 µsview code
+ 104 %
if and elseif (using ===)
Total time: 332 µsview code
+ 100 %
if, elseif and else (using ===)
Total time: 318 µsview code
+ 161 %
switch / case
Total time: 513 µsview code
+ 160 %
switch / case / default
Total time: 510 µsview code
Conclusion:
Using a switch/case or if/elseif is almost the same. Note that the test is unsing === (is exactly equal to) and is slightly faster then using == (is equal to).
Using the =&-ref-operator$obj = $someClass->f() vs. $obj =& $someClass->f()
Is a good idea to use the =&-ref-operator when calling a function in an object? Call 1'000x
+ 100 %
$obj = $someClass->f();
Total time: 1129 µsview code
+ 215 %
$obj =& $someClass->f();
Total time: 2422 µsview code
Conclusion:
Unless your extremely worried about how much RAM your using, leaving the &-ref-operator out seems like the slightly faster option.
Using the &-ref-operator...as a so called "alias"
Is a good idea to use the &-ref-operator to substitute (or alias) a complex mutidim-array? . Call 1'000x
E.g. $person = &$aHach["country"]["zip"]["street"]["number"]["name"]
+ 199 %
$alias = $aSingleDimArray[$i]
Total time: 1230 µsview code
+ 100 %
$alias = &$aSingleDimArray[$i]
Total time: 617 µsview code
+ 213 %
$alias = $aMultiDimArray[$i]["aaaaa"]["aaaaaaaaaa"]
Total time: 1316 µsview code
+ 408 %
$alias = &$aMultiDimArray[$i]["aaaaa"]["aaaaaaaaaa"]
Total time: 2517 µsview code
+ 251 %
$alias = veryMultiDimArray[$i]["a"]["aa"]["aaa"]["aaaa"]["aaaaa"]
Total time: 1546 µsview code
+ 812 %
$alias = &$veryMultiDimArray[$i]["a"]["aa"]["aaa"]["aaaa"]["aaaaa"]
Total time: 5013 µsview code
Conclusion:
Whilst only using a one dimensional array, it's actually faster to use an alias, but anything larger will result in a performance drop.
Read Loop:foreach() vs. for() vs. while(list() = each())
What is the best way to loop a hash array?
Given is a Hash array with 100 elements, 24byte key and 10k data per entry
+ 100 %
foreach($aHash as $val);
Total time: 24 µsview code
+ 742 %
while(list(,$val) = each($aHash));
Total time: 177 µsview code
+ 138 %
foreach($aHash as $key => $val);
Total time: 33 µsview code
+ 847 %
while(list($key,$val) = each($aHash));
Total time: 202 µsview code
+ 302 %
foreach($aHash as $key=>$val) $tmp[] = $aHash[$key];
Total time: 72 µsview code
+ 919 %
while(list($key) = each($aHash)) $tmp[] = $aHash[$key];
Total time: 219 µsview code
+ 323 %
Get key-/ value-array: foreach($aHash as $key[]=>$val[]);
Total time: 77 µsview code
+ 340 %
Get key-/ value-array: array_keys() / array_values()
Total time: 81 µsview code
+ 424 %
$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $tmp[] = $aHash[$key[$i]];
Total time: 101 µsview code
Conclusion:
In all cases I've found that the foreach loop is substantially faster than both the while() and for() loop procedures. One thing to note is that when using an entire loop from the start it's extremely good to use the reset() function in all examples
Given that the previous version of the tests have been very controvercial and incorrect, I must appologise for forgetting to implement the reset() function to allow the while() loops to start from the beginning instead of the end. Thanks to Anthony Bush for spotting this out.
Quote Typesdouble (") vs. single (') quotes
Is a there a difference in using double (") and single (') quotes for strings. Call 1'000x
+ 102 %
single (') quotes. Just an empty string: $tmp[] = '';
Total time: 664 µsview code
+ 100 %
double (") quotes. Just an empty string: $tmp[] = "";
Total time: 650 µsview code
+ 114 %
single (') quotes. 20 bytes Text : $tmp[] = 'aaaaaaaaaaaaaaaaaaaa';
Total time: 743 µsview code
+ 126 %
double (") quotes. 20 bytes Text : $tmp[] = "aaaaaaaaaaaaaaaaaaaa";
Total time: 819 µsview code
+ 114 %
single (') quotes. 20 bytes Text and 3x a $ : $tmp[] = 'aa $ aaaa $ aaaa $ a';
Total time: 740 µsview code
+ 114 %
double (") quotes. 20 bytes Text and 3x a $ : $tmp[] = "aa $ aaaa $ aaaa $ a";
Total time: 744 µsview code
+ 111 %
double (") quotes. 20 bytes Text and 3x a \$ : $tmp[] = "aa \$ aaaa \$ aaaa \$ a";
Total time: 720 µsview code
Conclusion:
In today's versions of PHP it looks like this argument has been satisfied on both sides of the line. Lets all join together in harmony in this one!
Counting LoopsFor-loop test
Is it worth the effort to calculate the length of the loop in advance?
e.g. "for ($i=0; $i<$size; $i++)" instead of "for ($i=0; $i<sizeOf($x); $i++)"
A loop with 1000 keys with 1 byte values are given.
+ 169 %
With pre calc - count()
Total time: 415 µsview code
+ 59542 %
Without pre calc - count()
Total time: 146502 µsview code
+ 100 %
With pre calc - sizeof()
Total time: 246 µsview code
+ 72921 %
Without pre calc - sizeof()
Total time: 179420 µsview code
Conclusion:
Unsurprising results... this is one of the easiest things to implement in any application and is the widest agreed upon benchmarking item within the online PHP community. The results basically speak for themselves.