Over a million developers have joined DZone.

PHP 5.3 is quicker than PHP 5.2

· Web Dev Zone

I know that everyone already knows this, but I happened to find out for myself recently!

I was looking at the way view helpers work in ZF2 and thought it would be more convenient if we altered the syntax a little. A side-effect of the change was that we'd have to use call_user_func_array, which is perceived as slow. I thought I'd whip up a simple test to find out how much slower it would be over a direct method call.

That is, how much slower is this code:

$a = new A();
call_user_func_array(array($a, 'b'), array(1));

than this code:

$a = new A();
$a->b(1);

Bear in mind that I don't do formal benchmarks and have no clue on methodology. Paul Jones is your man for proper testing of performance.

With that caveat out of the way this is the code I wrote to test:

benchmark.php

<?php

class A
{
function b($a)
{
return;
}
}

define ('ITERATIONS', 10000000);
$start = microtime(true);
$a = new A();
for ($i = 0; $i < ITERATIONS; ++$i) {
$a->b(1);
}
$stop = microtime(true);
echo 'Direct method call: ' . ($stop - $start) . ' seconds' . PHP_EOL;

$start = microtime(true);
for ($i = 0; $i < ITERATIONS; ++$i) {
call_user_func_array(array($a, 'b'), array(1));
Direct method call: 8.3424661159515 seconds
call_user_func_array call: 26.904649972916 seconds}
$stop = microtime(true);
echo 'call_user_func_array call: ' . ($stop - $start) . ' seconds' . PHP_EOL;

This was the simplest possible scenario I could imagine and so would show call_user_func_array in the best possible light.

I ran the test on my laptop (using the command line) and got these results:

Direct method call: 21.155211925507 seconds
call_user_func_array call: 72.147792100906 seconds

i.e. call_user_func_array is around 3.5 times slower over 10 million iterations.

Nobody uses their laptop to serve a website though! Being the curious sort, I thought I'd test the difference of this script between PHP 5.2 and PHP 5.3 in a completely unscientific way as I'm lazy! I happen to have a server with both PHP 5.2 and PHP 5.3. on it, so I used that:

PHP 5.2:

Direct method call: 8.3424661159515 seconds
call_user_func_array call: 26.904649972916 seconds

PHP 5.3:

Direct method call: 3.35181903839 seconds
call_user_func_array call: 11.5989868641 seconds

As you can see, my server is much faster than my laptop :) Also, the relative difference is the same. i.e. PHP 5.3's call_user_func_array is as slow as it is in PHP 5.2.

I think I'll move everything to PHP 5.3 for the free performance gain! Of course, The question remains as to whether we should avoid call_user_func_array or not.

I would however recommend testing your own apps and see if it makes any difference and please don't take this post as anything other than something interesting I found out!

Topics:

Published at DZone with permission of Rob Allen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}