String Outputecho vs. print
Is a there a difference between what option you use to output your content?. Called within Output Buffering 1'000x
+ 191 %
echo 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'
Total time: 200 µsview code
+ 224 %
print 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'
Total time: 235 µsview code
+ 606 %
echo 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa'
Total time: 636 µsview code
+ 545 %
echo 'aaaaaaa','aaaaaaa','aaaaaaa','aaaaaaa'
Total time: 572 µsview code
+ 639 %
print 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa'
Total time: 670 µsview code
+ 780 %
$a = 'aaaaaaa';
echo 'aaaaaaa'.$a.'aaaaaaa'.$a
Total time: 818 µsview code
+ 722 %
$a = 'aaaaaaa';
echo 'aaaaaaa',$a,'aaaaaaa',$a
Total time: 757 µsview code
+ 808 %
$a = 'aaaaaaa';
print 'aaaaaaa'.$a.'aaaaaaa'.$a
Total time: 848 µsview code
+ 770 %
$a = 'aaaaaaa';
echo $a.$a.$a.$a
Total time: 808 µsview code
+ 743 %
$a = 'aaaaaaa';
echo $a,$a,$a,$a
Total time: 779 µsview code
+ 767 %
$a = 'aaaaaaa';
print $a,$a,$a,$a
Total time: 804 µ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
+ 121 %
isSet() with var that was set
Total time: 233 µsview code
+ 124 %
empty() with var that was set
Total time: 239 µsview code
+ 100 %
isSet() with var that was *not* set
Total time: 192 µsview code
+ 100 %
empty() with var that was *not* set
Total time: 193 µsview code
+ 107 %
isSet() with array-var that was set
Total time: 206 µsview code
+ 133 %
empty() with array-var that was set
Total time: 255 µsview code
+ 100 %
isSet() with array-var that was *not* set
Total time: 193 µsview code
+ 103 %
empty() with array-var that was *not* set
Total time: 197 µsview code
+ 535 %
is_array() of an array
Total time: 1028 µsview code
+ 532 %
is_array() of a string
Total time: 1023 µsview code
+ 1072 %
is_array() of a non set value
Total time: 2060 µsview code
+ 1121 %
isSet() AND is_array() of a non set value
Total time: 2155 µ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))
Quote Typesdouble (") vs. single (') quotes
Is a there a difference in using double (") and single (') quotes for strings. Call 1'000x
+ 223 %
single (') quotes. Just an empty string: $tmp[] = '';
Total time: 724 µsview code
+ 100 %
double (") quotes. Just an empty string: $tmp[] = "";
Total time: 324 µsview code
+ 159 %
single (') quotes. 20 bytes Text : $tmp[] = 'aaaaaaaaaaaaaaaaaaaa';
Total time: 516 µsview code
+ 118 %
double (") quotes. 20 bytes Text : $tmp[] = "aaaaaaaaaaaaaaaaaaaa";
Total time: 382 µsview code
+ 120 %
single (') quotes. 20 bytes Text and 3x a $ : $tmp[] = 'aa $ aaaa $ aaaa $ a';
Total time: 389 µsview code
+ 116 %
double (") quotes. 20 bytes Text and 3x a $ : $tmp[] = "aa $ aaaa $ aaaa $ a";
Total time: 375 µsview code
+ 119 %
double (") quotes. 20 bytes Text and 3x a \$ : $tmp[] = "aa \$ aaaa \$ aaaa \$ a";
Total time: 387 µ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!
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"]
+ 177 %
$alias = $aSingleDimArray[$i]
Total time: 664 µsview code
+ 100 %
$alias = &$aSingleDimArray[$i]
Total time: 375 µsview code
+ 211 %
$alias = $aMultiDimArray[$i]["aaaaa"]["aaaaaaaaaa"]
Total time: 791 µsview code
+ 662 %
$alias = &$aMultiDimArray[$i]["aaaaa"]["aaaaaaaaaa"]
Total time: 2482 µsview code
+ 265 %
$alias = veryMultiDimArray[$i]["a"]["aa"]["aaa"]["aaaa"]["aaaaa"]
Total time: 992 µsview code
+ 1329 %
$alias = &$veryMultiDimArray[$i]["a"]["aa"]["aaa"]["aaaa"]["aaaaa"]
Total time: 4985 µ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.
Counting LoopsFor vs. While
Is there an actual difference between counting up between the for loop and the while loop?
+ 113 %
for($i = 0; $i < 1000000; ++$i);
Total time: 81678 µsview code
+ 100 %
$i = 0; while($i < 1000000) ++$i;
Total time: 72362 µsview code
Conclusion:
Well there you have it, the while loop 90% of the time is indeed slightly faster
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: 678 µsview code
+ 213 %
$obj =& $someClass->f();
Total time: 1447 µsview code
Conclusion:
Unless your extremely worried about how much RAM your using, leaving the &-ref-operator out seems like the slightly faster option.
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: 15 µsview code
+ 986 %
while(list(,$val) = each($aHash));
Total time: 148 µsview code
+ 140 %
foreach($aHash as $key => $val);
Total time: 21 µsview code
+ 1052 %
while(list($key,$val) = each($aHash));
Total time: 158 µsview code
+ 346 %
foreach($aHash as $key=>$val) $tmp[] = $aHash[$key];
Total time: 52 µsview code
+ 1125 %
while(list($key) = each($aHash)) $tmp[] = $aHash[$key];
Total time: 169 µsview code
+ 398 %
Get key-/ value-array: foreach($aHash as $key[]=>$val[]);
Total time: 60 µsview code
+ 367 %
Get key-/ value-array: array_keys() / array_values()
Total time: 55 µsview code
+ 586 %
$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $tmp[] = $aHash[$key[$i]];
Total time: 88 µ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.
Control Structuresswitch/case/default vs. if/elseif/else
Is a there a difference between switch and if structures?. Call 1'000x
+ 119 %
if and elseif (using ==)
Total time: 190 µsview code
+ 131 %
if, elseif and else (using ==)
Total time: 208 µsview code
+ 103 %
if and elseif (using ===)
Total time: 163 µsview code
+ 100 %
if, elseif and else (using ===)
Total time: 159 µsview code
+ 126 %
switch / case
Total time: 200 µsview code
+ 143 %
switch / case / default
Total time: 228 µ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).
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.
+ 122 %
With pre calc - count()
Total time: 242 µsview code
+ 56556 %
Without pre calc - count()
Total time: 111917 µsview code
+ 100 %
With pre calc - sizeof()
Total time: 198 µsview code
+ 53117 %
Without pre calc - sizeof()
Total time: 105111 µ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.
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.
+ 535 %
foreach($aHash as $key=>$val) $aHash[$key] .= "a";
Total time: 385 µsview code
+ 229 %
while(list($key) = each($aHash)) $aHash[$key] .= "a";
Total time: 165 µsview code
+ 100 %
$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= "a";
Total time: 72 µsview code
Conclusion:
Proof in this example shows how functionally murderous the foreach() loop can be.