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. |
Test: READ LOOP: foreach() 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 I've chosen the large data amount to try out what happens if I reference the data with the &-ref-operator (to avoid copying). But to my surprise the loops are never faster! In tests 5 and 6 are even 10x - 30x slower !! The larger the data entrys are the slower the tests 5 and 6 get! Copying seams always faster then using the &-ref-operator. Way ??? Let me know at bs_php@users.sourceforge.net | ||
+ 100 % | 1: foreach($aHash as $val); | Total time: 0[ms] |
+ 1096 % | 2: while(list(,$val) = each($aHash)); | Total time: 1[ms] |
+ 2960 % | 3: foreach($aHash as $key=>$val); | Total time: 1[ms] |
+ 2696 % | 4: while(list($key,$val)= each($aHash)); | Total time: 1[ms] |
+ 2475 % | 5: foreach($aHash as $key=>$val) $tmp[] = &$aHash[$key]; | Total time: 1[ms] |
+ 2198 % | 6: while(list($key) = each($aHash)) $tmp[]=&$aHash[$key]; | Total time: 1[ms] |
+ 1027 % | 7: Get key-/ value-array: foreach($aHash as $key[]=>$val[]); | Total time: 0[ms] |
+ 569 % | 8: Get key-/ value-array: array_keys() / array_values() | Total time: 0[ms] |
+ 1167 % | 9: STRANGE: This is the fasetest code when using the the &-ref-operator (to avoid copying) $key = array_keys($aHash); $size = sizeOf($key); for ($i=0; $i<$size; $i++) $tmp[] = &$aHash[$key[$i]]; | Total time: 1[ms] |
Conclusion: It must have something to do with PHP4 variable ref-count So you can safely use foreach and only use the &-ref-operator when realy needed OR (according to the link above) when passing objects to functions. (Thanx to Wayne for his help) |
Test: MODIFY LOOP: foreach() vs. while(list()=each()) | ||
---|---|---|
While the above test only reads and copies the data the question arised what would happen if I modify each value of the hash above. Again I an unexpected result. Even if I reduce the data size to 100 byte p. e. it ends up that Nr.3 is 1.5 - 2x faster. | ||
+ 240 % | 1: foreach($aHash as $key=>$val) $aHash[$key] .= "a"; | Total time: 1[ms] |
+ 100 % | 2: while(list($key) = each($aHash)) $aHash[$key] .= "a"; | Total time: 0[ms] |
+ 128 % | 3: STRANGE: This is the fasetest code : $key = array_keys($aHash); $size = sizeOf($key); for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= "a"; | Total time: 1[ms] |
Conclusion: Use foreach unless the hash is lage AND has lage data elements. In that case use variation Nr.3 . |
Test: 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++)" | ||
+ 100 % | 1: With pre calc | Total time: 0[ms] |
+ 1483 % | 2: Without pre calc | Total time: 4[ms] |
Conclusion: The test above speeks for it self. Always calculate the length of the loop in advance! |
Test: Using the &-ref-operator as 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"]["streat"]["number"]["name"] | ||
+ 175 % | 1: NO Aliasing. Using: $aSingleDimArray[$i] | Total time: 0[ms] |
+ 100 % | 2: Aliasing. Using: $alias = &$aSingleDimArray[$i] | Total time: 0[ms] |
+ 249 % | 3: NO Aliasing. Using: $aMultiDimArray[$i]["aaaaa"]["aaaaaaaaaa"] | Total time: 1[ms] |
+ 124 % | 4: Aliasing. Using: $alias = &$aMultiDimArray[$i]["aaaaa"]["aaaaaaaaaa"] | Total time: 0[ms] |
+ 361 % | 5: NO Aliasing. Using: veryMultiDimArray[$i]["a"]["aa"]["aaa"]["aaaa"]["aaaaa"] | Total time: 1[ms] |
+ 235 % | 6: Aliasing. Using: $alias = &$veryMultiDimArray[$i]["a"]["aa"]["aaa"]["aaaa"]["aaaaa"] | Total time: 1[ms] |
Conclusion: It seams to be ok to use aliases. It also makes the code more readabel. But I was expecting to get a lager performance gain; especially with very multdimetional arrays. |
Test: $obj = new SomeClass() vs. $obj =& new SomeClass() using the =&-ref-operator | ||
---|---|---|
Is a good idea to use the =&-ref-operator when creating a new object? Call 1'000x | ||
+ 100 % | 1: $obj = new SomeClass() | Total time: 0[ms] |
+ 188 % | 2: $obj =& new SomeClass() | Total time: 1[ms] |
+ 1708 % | 3: $obj =& $someClass->f(); | Total time: 5[ms] |
+ 195 % | 4: $obj = $someClass->f(); | Total time: 1[ms] |
Conclusion: There seams to be no difference in performance. |
Test: double (") vs. single (') quotes | ||
---|---|---|
Is a there a difference in using double (") and single (') quotes for strings. Call 1'000x | ||
+ 2625 % | 1: single (') quotes. Just an empty string: $tmp[] = ''; | Total time: 0[ms] |
+ 5850 % | 2: double (") quotes. Just an empty string: $tmp[] = ""; | Total time: 0[ms] |
+ 100 % | 3: single (') quotes. 20 bytes Text : $tmp[] = 'aaaaaaaaaaaaaaaaaaaa'; | Total time: 0[ms] |
+ 2525 % | 4: double (") quotes. 20 bytes Text : $tmp[] = "aaaaaaaaaaaaaaaaaaaa"; | Total time: 0[ms] |
+ 2550 % | 5: single (') quotes. 20 bytes Text and 3x a $ : $tmp[] = 'aa $ aaaa $ aaaa $ a'; | Total time: 0[ms] |
+18025 % | 6: double (") quotes. 20 bytes Text and 3x a $ : $tmp[] = "aa $ aaaa $ aaaa $ a"; | Total time: 1[ms] |
+ 2775 % | 7: double (") quotes. 20 bytes Text and 3x a \$ : $tmp[] = "aa \$ aaaa \$ aaaa \$ a"; | Total time: 0[ms] |
Conclusion: Single and double quoted strings behave almost the same with one exception: Don't use the a lonely ($) in double quoted string unless you want to reference a PHP-var; or use (\$). |
Test: isSet() vs. empty() vs.   is_array() | ||
---|---|---|
What is the performance of isSet() and empty(). Call 2'000x | ||
+ 412 % | 1: isSet() with var that was set | Total time: 0[ms] |
+ 161 % | 2: empty() with var that was set | Total time: 0[ms] |
+ 102 % | 3: isSet() with var that was *not* set | Total time: 0[ms] |
+ 100 % | 4: empty() with var that was *not* set | Total time: 0[ms] |
+ 116 % | 5: isSet() with array-var that was set | Total time: 0[ms] |
+ 105 % | 6: empty() with array-var that was set | Total time: 0[ms] |
+ 107 % | 7: isSet() with array-var that was *not* set | Total time: 0[ms] |
+ 319 % | 8: empty() with array-var that was *not* set | Total time: 0[ms] |
+ 206 % | 9: is_array() of an array | Total time: 0[ms] |
+ 200 % | 10: is_array() of a string | Total time: 0[ms] |
+ 8705 % | 11: is_array() of a non set value | Total time: 9[ms] |
+ 233 % | 12: isSet() AND is_array() of a non set value | Total time: 0[ms] |
Conclusion: isSet() and empty() are identical. Interesting that a is_array() on a unset val is 3x slower. So alway check if val is set at all befor using type-checking. E.g. if (isSet($foo) AND is_array($foo)) |
Test: switch/case vs. if/elseif | ||
---|---|---|
Is a there a difference between switch and if elseif. Call 1'000x | ||
+ 100 % | 1: if and elseif (using ==) | Total time: 0[ms] |
+ 200 % | 2: if and elseif (using ===) | Total time: 0[ms] |
+ 126 % | 3: case | Total time: 0[ms] |
Conclusion: Using a switch/case or if/elseif is almost the same. Note that the test is unsing === and is slitly faster then using ==. |