Proof that PHP 5.4 is Twice as Fast as PHP 5.3

So recently I was working on some benchmarks for different versions of PHP, because I heard that PHP 5.4 is “faster” and since I’m a data geek I want to know how much faster! Now, PHP 5.4 is, in general, faster than PHP 5.3 but not twice as fast* unless you pick a use case which has been particularly optimised.

My first attempt at benchmarking the two versions produced this:

graph showing php 5.4 taking half the time of php 5.3

This was a surprise to me; was PHP 5.4 really so much faster??

It turns out that it isn’t, and my mistake was to pick a very simple script which only does one thing – and that thing is something that actually was vastly improved between versions! I have a script that runs the same piece of code lots of times and does some crude timings with that – so these numbers are only useful as a relative comparison; I’m just running this stuff on my laptop.

The script:

$r = 10 * 1000 * 1000;
$start = microtime(true);

while($r-- > 0) {
    new StdClass();
}

$finish = microtime(true);
echo "time taken: " . ($finish - $start) . "\n";

I ran each test five times and averaged the results (original data: thrown into a gist if you want it).

The moral of this story is: you can prove anything you want with statistics! I’m assembling a more varied script for some rather more useful benchmarks, but just look at how much faster it is to instantiate an object in PHP 5.4! Many thanks to everyone who contributes to the PHP project, the language continues to evolve and improve in so many ways, and I am grateful!

* Apologies for the inflammatory blog post title, but those were my exact words when I got this result set.

20 thoughts on “Proof that PHP 5.4 is Twice as Fast as PHP 5.3

  1. Supposing that this article is correct it sounds like there could be a number of performance enhancements in 5.4, especially where tight and disciplined coding practices are employed – like avoiding the practice of adding new properties dynamically to objects.

    There’s some good work going on with PHP, it’s a great language to work with.

  2. Pingback: Programowanie w PHP » Blog Archive » Lorna Mitchell’s Blog: Proof that PHP 5.4 is Twice as Fast as PHP 5.3

  3. Pingback: PHP 5.4 e prestazioni | Edit - Il blog di HTML.it

  4. Pingback: PHP 5.4 e prestazioni | Pixelset.it

  5. I’m glad you mentioned that the test was on a simple script. As with all new versions of PHP (any language) there is a push for performance improvements and its good to see the gains in a visual like this.
    It would interesting to run the script and others on every version of PHP and see the jumps in progress in a similar visual.

  6. Just ran this test on my machine, interestingly casting (object) array is much quicker than creating stdClass:

    5.3.1
    new stdClass(): 5.9607532024384
    (object) array(): 3.4466769695282

    5.4.3
    new stdClass(): 3.2519390583038
    (object) array(): 2.7573947906494

  7. Pingback: PHP 5.4 Benchmarks | LornaJane

  8. Nice! I’ve had similar results and always recommend client to update to 5.4. Now with 5.5 just around the corner it will be interesting to see how it stacks up. :)

  9. Pingback: Benchmarking HipHopVM 2.1.0 against PHP 5.3.10 on Ubuntu 12.04 LTS | LeaseWeb Labs

  10. Pingback: Benchmarking HipHopVM 2.2.0: slower, but more compatible | LeaseWeb Labs

  11. Pingback: Why Facebook is making a PHP renaissance | KidsIL

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.