- development
- php
Switching to PHP7
Recently PHP7.0 was released much to the delight of myself. Along with a slew of new features, PHP7 integrated the php-ng project into the fold which resulted in much quicker execution time for most PHP applications due the amount of refactoring in the main code base.
In order to get a better understanding of what sort of speed improvements can be achieved by some simple changes to both the code base as well as PHP versions, I ran 3 different versions of php and measured the differences between each. For the sake of this post, i have only included PHP5.3 and PHP7.0. My PHP5.6 tests were over multiple machines giving different results at the time while still under heavy development.
But first, here are the major changes in the versions:
[v1] - Built using OOP classes and require statements
[v2] - Partial PSR-4 implementation on existing system
[v3] - Rebuilt full system with full PSR-4 autoloading
[v4] - Minor Tweaks and adjustments
Now you might be wondering why i haven't included any graph points for v1. Well this is for a very simple reason... It majorly skews the graph. The results observed for v1 was 0.04 seconds, which in the scale of 7ms to 0.5ms makes everything look REALLY small.
Ok, so the actual reason was because v1 was still built using a require_once
and include_once
system without any form of autoloading. This is inherently taxing and causes a massive amount of overhead when running an application because every possible file will be loaded at any given time. The benefit of running an autoloader, is so that you're only loading the files that are absolutely necissary at runtime.
So here is a basic timescale for the results:
v1 PHP 5.3 - 0.04 seconds = 40.0 ms
v2 PHP 5.3 - 0.007 seconds = 7.0 ms
v2 PHP 7.0 - 0.0025 seconds = 2.5 ms -nightly build
v3 PHP 5.3 - 0.004 seconds = 4.0 ms
v3 PHP 7.0 - 0.0005 seconds = 0.5 ms
v4 PHP 7.0 - 0.0008 seconds = 0.8 ms
As you can see from the results, PHP 7 actually decreases the processing time quite dramatically without any real optimisations on my end. I have had people ask me questions like:
Where did you get your improvments? Was it in scalar type hinting, optimisations etc?
I guess the answer to the questions is... no. I actually did very little in terms of optimising specifically for PHP7. The only sort of changes i made were making things easier to read and understand with the process.
A large portion of the processing time was converting to PHP FIG's PSR-4, but literally just switching out PHP versions is what really made the difference.
In actual fact, this is my script i was using to switch versions for just testing PHP timings.
#!/bin/bash
ARG="$1"
ENMOD=/usr/sbin/a2enmod
DISMOD=/usr/sbin/a2dismod
if [ "$ARG" = "" ]; then
echo -e "\tUsage ./switchphp (php5|php7)";
exit 0;
fi;
if [ "$ARG" = "php7" ]; then
$DISMOD php5;
$ENMOD php7;
else
$DISMOD php7;
$ENMOD php5;
fi;
/etc/init.d/apache2 restart
Final Thoughts
I guess what has really opened my eye, is the fact that PHP has undergone a very rigorous shakedown, has lost a few kilos of bloat and is now ready to compete again in the modern web.
I have been very pleased with not only the performance boost, but some features such as the scalar type hinting (which have been desperately needed for some time) and the deprecation of strict standards from fatal errors to warnings.
While it's not the most perfect scripting language around, i hope that this will give people another option for a lightweight application/scripting option for servers using a very versatile language.