menu

秋梦无痕

一场秋雨无梦痕,春夜清风冻煞人。冬来冷水寒似铁,夏至京北蟑满城。

Avatar

The PHP Benchmark

from: http://www.phpbench.com/

The PHP Benchmark
==================================
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
----------------------------------
foreach($aHash as $val); Total time: 18 μs
while(list(,$val) = each($aHash)); Total time: 91 μs
foreach($aHash as $key => $val); Total time: 18 μs
while(list($key,$val) = each($aHash)); Total time: 97 μs
foreach($aHash as $key=>$val) $tmp[] = $aHash[$key]; Total time: 43 μs
while(list($key) = each($aHash)) $tmp[] = $aHash[$key]; Total time: 111 μs
Get key-/ value-array: foreach($aHash as $key[]=>$val[]); Total time: 53 μs
Get key-/ value-array: array_keys() / array_values() Total time: 46 μs
$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $tmp[] = $aHash[$key[$i]]; Total time: 60 μs
----------------------------------
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.
==================================
Counting Loops
For-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 100000 keys with 1 byte values are given.
----------------------------------
With pre calc - count() Total time: 8 μs
Without pre calc - count() Total time: 3 μs
With pre calc - sizeof() Total time: 4 μs
Without pre calc - sizeof() Total time: 3 μs
----------------------------------
Conclusion:
Surprising results show that if you implement sizeof() there is almost no difference in whether calculating the size of a loop in advance
==================================
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.
----------------------------------
foreach($aHash as $key=>$val) $aHash[$key] .= "a"; Total time: 683 μs
while(list($key) = each($aHash)) $aHash[$key] .= "a"; Total time: 174 μs
$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= "a"; Total time: 116 μs
----------------------------------
Conclusion:
Proof in this example shows how functionally murderous the foreach() loop can be.
==================================
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"]
----------------------------------
$alias = $aSingleDimArray[$i] Total time: 780 μs
$alias = &$aSingleDimArray[$i] Total time: 306 μs
$alias = $aMultiDimArray[$i]["aaaaa"]["aaaaaaaaaa"] Total time: 811 μs
$alias = &$aMultiDimArray[$i]["aaaaa"]["aaaaaaaaaa"] Total time: 1259 μs
$alias = veryMultiDimArray[$i]["a"]["aa"]["aaa"]["aaaa"]["aaaaa"] Total time: 970 μs
$alias = &$veryMultiDimArray[$i]["a"]["aa"]["aaa"]["aaaa"]["aaaaa"] Total time: 2533 μs
----------------------------------
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.
==================================
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
----------------------------------
$obj = $someClass->f(); Total time: 509 μs
$obj =& $someClass->f(); Total time: 1130 μs
----------------------------------
Conclusion:
Unless your extremely worried about how much RAM your using, leaving the &-ref-operator out seems like the slightly faster option.
==================================
String Output
echo vs. print
----------------------------------
Is a there a difference between what option you use to output your content?. Called within Output Buffering 1'000x
----------------------------------
echo '' Total time: 124 μs
print '' Total time: 113 μs
echo 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa' Total time: 138 μs
print 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa' Total time: 175 μs
echo 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa' Total time: 148 μs
echo 'aaaaaaa','aaaaaaa','aaaaaaa','aaaaaaa' Total time: 360 μs
print 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa' Total time: 160 μs
$a = 'aaaaaaa';
echo 'aaaaaaa'.$a.'aaaaaaa'.$a Total time: 611 μs
$a = 'aaaaaaa';
echo 'aaaaaaa',$a,'aaaaaaa',$a Total time: 460 μs
$a = 'aaaaaaa';
print 'aaaaaaa'.$a.'aaaaaaa'.$a Total time: 599 μs
$a = 'aaaaaaa'
echo $a.$a.$a.$a Total time: 596 μs
$a = 'aaaaaaa';
echo $a,$a,$a,$a Total time: 471 μs
$a = 'aaaaaaa';
print $a,$a,$a,$a Total time: 626 μs
----------------------------------
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.
==================================
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
----------------------------------
$obj = new SomeClass(); Total time: 665 μs
$obj =& new SomeClass(); Total time: 621 μs
----------------------------------
Conclusion:
There seams to be no difference in performance.
==================================
Control Structures
switch/case/default vs. if/elseif/else
----------------------------------
Is a there a difference between switch and if structures?. Call 1'000x
----------------------------------
if and elseif (using ==) Total time: 240 μs
if, elseif and else (using ==) Total time: 232 μs
if and elseif (using ===) Total time: 166 μs
if, elseif and else (using ===) Total time: 171 μs
switch / case Total time: 234 μs
switch / case / default Total time: 261 μs
----------------------------------
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 Loops
For vs. While
----------------------------------
Is there an actual difference between counting up between the for loop and the while loop?
----------------------------------
for($i = 0; $i < 1000000; ++$i); Total time: 109792 μs
$i = 0; while($i < 1000000) ++$i; Total time: 78218 μs
----------------------------------
Conclusion:
Well there you have it, the while loop 90 of the time is indeed slightly faster
==================================
Variable Type Checking
isSet() vs. empty() vs. is_array()
----------------------------------
What is the performance of isSet() and empty(). Call 2'000x
----------------------------------
isSet() with var that was set Total time: 324 μs
empty() with var that was set Total time: 336 μs
isSet() with var that was *not* set Total time: 248 μs
empty() with var that was *not* set Total time: 233 μs
isSet() with array-var that was set Total time: 322 μs
empty() with array-var that was set Total time: 321 μs
isSet() with array-var that was *not* set Total time: 228 μs
empty() with array-var that was *not* set Total time: 274 μs
is_array() of an array Total time: 703 μs
is_array() of a string Total time: 642 μs
is_array() of a non set value Total time: 1732 μs
isSet() AND is_array() of a non set value Total time: 1830 μs
----------------------------------
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 Types
double (") vs. single (') quotes
----------------------------------
Is a there a difference in using double (") and single (') quotes for strings. Call 1'000x
----------------------------------
single (') quotes. Just an empty string: $tmp[] = ''; Total time: 408 μs
double (") quotes. Just an empty string: $tmp[] = ""; Total time: 375 μs
single (') quotes. 20 bytes Text : $tmp[] = 'aaaaaaaaaaaaaaaaaaaa'; Total time: 398 μs
double (") quotes. 20 bytes Text : $tmp[] = "aaaaaaaaaaaaaaaaaaaa"; Total time: 377 μs
single (') quotes. 20 bytes Text and 3x a $ : $tmp[] = 'aa $ aaaa $ aaaa $ a'; Total time: 370 μs
double (") quotes. 20 bytes Text and 3x a $ : $tmp[] = "aa $ aaaa $ aaaa $ a"; Total time: 374 μs
double (") quotes. 20 bytes Text and 3x a \$ : $tmp[] = "aa \$ aaaa \$ aaaa \$ a"; Total time: 372 μs
----------------------------------
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!

1300~

我只是来抢楼的~

评论已关闭