PHP 7.1, 7.2, 7.3 and 7.4 benchmarks and comparisons

Putting PHP versions 7.1, 7.2, 7.3 and 7.4 against each other in a simple benchmark driven comparison for time taken to do calculations.

Results are from using this benchmark script. Value is in seconds, lower is better.

Version7.17.27.37.4
Math0.1160.1060.1070.117
String0.1810.1610.1480.145
Loops0.0090.0080.0080.006
If else0.0160.0090.0100.006
Calculation total0.3210.2850.2720.273
MySQL total0.2300.2240.2330.227
TOTAL0.5510.5090.5060.500

Test were ran on a Vultr $6 p/m high-frequency instance (1CPu, 1GB mem) with Ubuntu 18.04 x64

The total times were as expected, the older version gets pipped by its successor. PHP 7.4 really is the fastest version although that was completely expected beforehand.

PHP 7.1 is from 2016 and since December 1st 2019 is no longer supported. This version is 3 years older than 7.4 and honestly doesn’t show a noticeable drop-off in its performance.

What is interesting is that PHP 7.4 match calculation was the slowest amongst the versions. The math calculation code is this:

function test_math(&$result, $count = 99999)
{
    $timeStart = microtime(true);
    $mathFunctions = array("abs", "acos", "asin", "atan", "bindec", "floor", "exp", "sin", "tan", "pi", "is_finite", "is_nan", "sqrt");
    for ($i = 0; $i < $count; $i++) {
        foreach ($mathFunctions as $function) {
            call_user_func_array($function, array($i));
        }
    }
    $result['benchmark']['math'] = timer_diff($timeStart) . ' sec.';
}

PHP 7.4 just could not do the equations better than its predecessors. Again this would not be noticeable without a number reading.

Ground was made up with PHP 7.4 in the if else test.

function test_ifelse(&$result, $count = 999999)
{
    $timeStart = microtime(true);
    for ($i = 0; $i < $count; $i++) {
        if ($i == -1) {
        } elseif ($i == -2) {
        } else {
            if ($i == -3) {
            }
        }
    }
    $result['benchmark']['ifelse'] = timer_diff($timeStart) . ' sec.';
}

It got a standout result compared to the other versions. Faster speed with conditional statements would likely be preferred to math calculations in todays PHP usage.

These performance results whilst stating which version is fastest doesn’t change the general consensus that staying up to date with versions is the right thing because of security and support.

PHP 7.4 is the fastest, it’s also the latest and in focus version for stability. If you’re not game for 7.4 at least be on version 7.2 or 7.3 and no less. PHP version 7.2 loses its support late 2020.

 

Benchmark result php 7.1
php 7.1 benchmark
Benchmark result php 7.2
php 7.2 benchmark
Benchmark result php 7.3
php 7.3 benchmark
Benchmark result php 7.4
php 7.4 benchmark