Realistic PHP tuning

Everybody preaches now & then about microbenchmarks and how you can speed up the output of your pages or your framework by the slightest fraction... this can help but in perspective of the granded scheme of things it does little to nothing.

I've done my own micro benchmarks over time, namely after realizing I could've achieved something a little faster with less code had I used array_map instead of a foreach loop which resulted in:

What I had was:

$another_list = array();
foreach( $list AS $item )
{
  $another_list[] = trim($item);
}
$list = $another_list;
unset($another_list);

Which could've been more elegantly written (and ran a little faster) if it were:

$list = array_map('trim',$list);

One of thos 'duh' moments... so wondering what the speed improvements would be, if any, I did some microbenchmarking:

Graph showing the comparison between different loop methods

The source for this graph can be viewed at http://codeb.us/download/graph_source.phps. OpenOffice seems to have screwed the Y axis up a bit, the right hand side represents the number of seconds it took for the test to complete, with the X axis being the numer of elements in the array.

But does this really matter? The fastest method of trimming all strings within an array of 12800 elements, performed 100 times took less than some well optimized large SQL queries. In reality most of the things people do microbenchmarks on will have no overall impact on how quickly pages load... even spread over a whole framework it'll likely to not be noticable to the user.

Lets take the pragmatic approach: pre-optimization is a sapper of programmer performance. Get your code written, write it well and focus on maintainability; then look at the application as a whole using great tools like xdebug and kcachegrind to find where your hot spots really are - and focus on that.


1 Responses to Realistic PHP tuning

  1. 3 Digging in a Habari sandpit 2008-02-13 10:08:51

    ... gentleman called Harry from London developed this attractive Habari theme. 0 Responses to now on Habari ...

Leave a Reply



Bookmarks

I'm constantly finding interesting stuff, here are some of the things I've bookmarked recently:

HarryR on Faves.com