Man of Honour
- Joined
- 31 Jan 2004
- Posts
- 16,338
- Location
- Plymouth
I can't remember how I found this page, but it's open in Firefox and it's pretty good reading.
It's just a few benchmarks on the tiny things - single quotes versus double quotes, foreach() versus while() and so on.
Quite straightforward and pretty unexpected results for those of us who know PHP pretty well, but it's nice to have some hard numbers (well...the page is re-run on each refresh...but still!) to show why it's better to use certain methods than others.
For example, you can easily see how using foreach() on a large array will dramatically slow down your program if you're using it to modify an array:
It's just a few benchmarks on the tiny things - single quotes versus double quotes, foreach() versus while() and so on.
Quite straightforward and pretty unexpected results for those of us who know PHP pretty well, but it's nice to have some hard numbers (well...the page is re-run on each refresh...but still!) to show why it's better to use certain methods than others.
For example, you can easily see how using foreach() on a large array will dramatically slow down your program if you're using it to modify an array:
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.
1:Total time: 15ms (+696%)Code:foreach($aHash as $key=>$val) $aHash[$key] .= "a";
2:Total time: 4ms (+177%)Code:while(list($key) = each($aHash)) $aHash[$key] .= "a";
3:
Total time: 2ms (+100%)Code:$key = array_keys($aHash); $size = sizeOf($key); for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= "a";