100% es el valor mas rápido.
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 = veryMultiDimArray[$i]["a"]["aa"]["aaa"]["aaaa"]["aaaaa"]
Total time: 580 µsview code
$alias = &$veryMultiDimArray[$i]["a"]["aa"]["aaa"]["aaaa"]["aaaaa"]
Total time: 1814 µsview code
Whilst only using a one dimensional array, it's actually faster to use an alias, but anything larger will result in a performance drop.
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.
$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= "a";
Total time: 40 µsview code
Proof in this example shows how functionally murderous the foreach() loop can be.
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
Unless your extremely worried about how much RAM your using, leaving the &-ref-operator out seems like the slightly faster option.
Control Structuresswitch/case/default vs. if/elseif/else
Is a there a difference between switch and if structures?. Call 1'000x
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 vs. While
Is there an actual difference between counting up between the for loop and the while loop?
Well there you have it, the while loop 90% of the time is indeed slightly faster
Variable Type CheckingisSet() vs. empty() vs. is_array()
What is the performance of isSet() and empty(). Call 2'000x
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))
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
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
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.
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 A loop with 1000 keys with 1 byte values are given.
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.
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
$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $tmp[] = $aHash[$key[$i]];
Total time: 40 µsview code
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
single (') quotes. 20 bytes Text : $tmp[] = 'aaaaaaaaaaaaaaaaaaaa';
Total time: 261 µsview code
double (") quotes. 20 bytes Text : $tmp[] = "aaaaaaaaaaaaaaaaaaaa";
Total time: 258 µsview code
single (') quotes. 20 bytes Text and 3x a $ : $tmp[] = 'aa $ aaaa $ aaaa $ a';
Total time: 250 µsview code
double (") quotes. 20 bytes Text and 3x a $ : $tmp[] = "aa $ aaaa $ aaaa $ a";
Total time: 254 µsview code
double (") quotes. 20 bytes Text and 3x a \$ : $tmp[] = "aa \$ aaaa \$ aaaa \$ a";
Total time: 249 µsview code
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!
Fuente: www.phpbench.com
No hay comentarios:
Publicar un comentario