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: 16 µsview code
+ 616 %
while(list(,$val) = each($aHash));
Total time: 100 µsview code
+ 112 %
foreach($aHash as $key => $val);
Total time: 18 µsview code
+ 616 %
while(list($key,$val) = each($aHash));
Total time: 100 µsview code
+ 235 %
foreach($aHash as $key=>$val) $tmp[] = $aHash[$key];
Total time: 38 µsview code
+ 735 %
while(list($key) = each($aHash)) $tmp[] = $aHash[$key];
Total time: 119 µsview code
+ 369 %
Get key-/ value-array: foreach($aHash as $key[]=>$val[]);
Total time: 60 µsview code
+ 309 %
Get key-/ value-array: array_keys() / array_values()
Total time: 50 µsview code
+ 376 %
$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $tmp[] = $aHash[$key[$i]];
Total time: 61 µ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.
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.
+ 100 %
With pre calc - count()
Total time: 214 µsview code
+ 44795 %
Without pre calc - count()
Total time: 95800 µsview code
+ 102 %
With pre calc - sizeof()
Total time: 219 µsview code
+ 57602 %
Without pre calc - sizeof()
Total time: 123189 µ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.
+ 390 %
foreach($aHash as $key=>$val) $aHash[$key] .= "a";
Total time: 414 µsview code
+ 103 %
while(list($key) = each($aHash)) $aHash[$key] .= "a";
Total time: 109 µsview code
+ 100 %
$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= "a";
Total time: 106 µsview code
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"]
+ 295 %
$alias = $aSingleDimArray[$i]
Total time: 882 µsview code
+ 100 %
$alias = &$aSingleDimArray[$i]
Total time: 299 µsview code
+ 288 %
$alias = $aMultiDimArray[$i]["aaaaa"]["aaaaaaaaaa"]
Total time: 863 µsview code
+ 385 %
$alias = &$aMultiDimArray[$i]["aaaaa"]["aaaaaaaaaa"]
Total time: 1153 µsview code
+ 321 %
$alias = veryMultiDimArray[$i]["a"]["aa"]["aaa"]["aaaa"]["aaaaa"]
Total time: 959 µsview code
+ 885 %
$alias = &$veryMultiDimArray[$i]["a"]["aa"]["aaa"]["aaaa"]["aaaaa"]
Total time: 2648 µ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.
String Outputecho vs. print
Is a there a difference between what option you use to output your content?. Called within Output Buffering 1'000x
+ 128 %
echo 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'
Total time: 141 µsview code
+ 160 %
print 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'
Total time: 176 µsview code
+ 134 %
echo 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa'
Total time: 147 µsview code
+ 322 %
echo 'aaaaaaa','aaaaaaa','aaaaaaa','aaaaaaa'
Total time: 354 µsview code
+ 144 %
print 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa'
Total time: 158 µsview code
+ 537 %
$a = 'aaaaaaa';
echo 'aaaaaaa'.$a.'aaaaaaa'.$a
Total time: 590 µsview code
+ 425 %
$a = 'aaaaaaa';
echo 'aaaaaaa',$a,'aaaaaaa',$a
Total time: 467 µsview code
+ 559 %
$a = 'aaaaaaa';
print 'aaaaaaa'.$a.'aaaaaaa'.$a
Total time: 614 µsview code
+ 541 %
$a = 'aaaaaaa';
echo $a.$a.$a.$a
Total time: 594 µsview code
+ 431 %
$a = 'aaaaaaa';
echo $a,$a,$a,$a
Total time: 474 µsview code
+ 592 %
$a = 'aaaaaaa';
print $a,$a,$a,$a
Total time: 651 µ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.
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: 240 µsview code
+ 136 %
if, elseif and else (using ==)
Total time: 230 µsview code
+ 100 %
if and elseif (using ===)
Total time: 169 µsview code
+ 101 %
if, elseif and else (using ===)
Total time: 171 µsview code
+ 139 %
switch / case
Total time: 235 µsview code
+ 173 %
switch / case / default
Total time: 293 µ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).
Variable Type CheckingisSet() vs. empty() vs. is_array()
What is the performance of isSet() and empty(). Call 2'000x
+ 144 %
isSet() with var that was set
Total time: 330 µsview code
+ 141 %
empty() with var that was set
Total time: 325 µsview code
+ 102 %
isSet() with var that was *not* set
Total time: 235 µsview code
+ 104 %
empty() with var that was *not* set
Total time: 238 µsview code
+ 140 %
isSet() with array-var that was set
Total time: 323 µsview code
+ 141 %
empty() with array-var that was set
Total time: 324 µsview code
+ 100 %
isSet() with array-var that was *not* set
Total time: 230 µsview code
+ 104 %
empty() with array-var that was *not* set
Total time: 240 µsview code
+ 290 %
is_array() of an array
Total time: 666 µsview code
+ 296 %
is_array() of a string
Total time: 681 µsview code
+ 824 %
is_array() of a non set value
Total time: 1894 µsview code
+ 791 %
isSet() AND is_array() of a non set value
Total time: 1819 µ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
+ 114 %
single (') quotes. Just an empty string: $tmp[] = '';
Total time: 429 µsview code
+ 100 %
double (") quotes. Just an empty string: $tmp[] = "";
Total time: 376 µsview code
+ 104 %
single (') quotes. 20 bytes Text : $tmp[] = 'aaaaaaaaaaaaaaaaaaaa';
Total time: 390 µsview code
+ 100 %
double (") quotes. 20 bytes Text : $tmp[] = "aaaaaaaaaaaaaaaaaaaa";
Total time: 375 µsview code
+ 102 %
single (') quotes. 20 bytes Text and 3x a $ : $tmp[] = 'aa $ aaaa $ aaaa $ a';
Total time: 382 µsview code
+ 100 %
double (") quotes. 20 bytes Text and 3x a $ : $tmp[] = "aa $ aaaa $ aaaa $ a";
Total time: 376 µsview code
+ 102 %
double (") quotes. 20 bytes Text and 3x a \$ : $tmp[] = "aa \$ aaaa \$ aaaa \$ a";
Total time: 382 µ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!
I think now would be the appropriate time for me to step in, introduce myself to the website programming arena and then explain the reasons why I made this website.
My name is
Chris Vincent (obviously). I'm a 19 year old living in
Brisbane, Australia and loving life out here. I'm one of those slightly geeky looking people who love to be outgoing and loves the fun times of life. As seen from my portfolio,
I make websites. I have been around the internet from the times of the tables (4 years ago?) and have since then picked up on a huge collection of skills ranging from PHP, to design, to project management/business skills. If you ask anyone whom I have ever worked with about how I approach my skills in work you'll see that I know of many in awe of my my skills, but in order to improve beyond your current level you have to aim your sights too far above your expectations so that you both forget to idolise those whom are much better and you also lose any sort of unrealised ego you seem to posess. Humbleness is not much of a talked about quality in the website industry but I believe that it is the key to success.
I am not going to now comment on anything what-so-ever related to micro-benchmarking because this is not the major purpose of phpbench.com, and it never has been. Initially phpbench.com was created just as a bit of fun to...
Test out my re-align skills on the
original php benchmark
Have a good play with best practises of extracting specific data from php files (hence the successful "view code" link
Attempt a small time project that would make better of my skills to format how I organised my code in a nicer way than my previous applications have been.
All in all micro-benchmarking was
not considered to me as something that Is utterly important for how a program works. I agree with those people who have said that the major items at the
hardware and algorithms. I've experienced these dilemmas quite a number of times in my own projects.
(For those who want an example of why this is the case, the cms that I constructed for one of my clients currently uses all these "bad" techniques and yet I'm more worried about a irremovable second query that currently would take about 1000% of the time compared to all the small snippet stock PHP syntax put together)
At this stage I'm going to introduce how I know Akash and why he was influenced to write
this article about my micro-benchmarking website. I have met Akash currently twice, once at a
Brisbane PHP Meetup and secondly at a
Brisbane BarCamp. He is a great guy with both a wicked mind and an some exceptional presentational skills for his age. At the BarCamp Akash kindly assisted me with a presentation (by controlling the computer) that took on the fact that as programmers we need to use caution when using old programming habits in these website
programming languages year after year because there is always the potential that in the case of the larger algorithms bottlenecks may appear because of the alterations in the bottom level application code. The reason I used phpbench.com as the example was precisely because of the confusion of the first set of results;
I have no idea why they differ so much but I used that to emphasise my presentational point about
an apparent difference between PHP4 and PHP5. From here it seems as though Akash liked the thought of micro-benchmarking and has written an article about it, in which I sincerely thank him for that.
To move right along, right now I am actually preparing myself to be included in that 1% of programmers who get given the opportunity to be exceptional coders. This whole experience has enlightened me a lot more than expected and from this I thank everyone.
(On a side note, if you know of people whom are up to this quality and live in Australia (preferably), please drop me a line...)