Development

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.

Version 7.1 7.2 7.3 7.4
Math 0.116 0.106 0.107 0.117
String 0.181 0.161 0.148 0.145
Loops 0.009 0.008 0.008 0.006
If else 0.016 0.009 0.010 0.006
Calculation total 0.321 0.285 0.272 0.273
MySQL total 0.230 0.224 0.233 0.227
TOTAL 0.551 0.509 0.506 0.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.

 

php 7.1 benchmark
php 7.2 benchmark
php 7.3 benchmark
php 7.4 benchmark
Share
Tags: PHPWeb Dev

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago