Is PHP 7.4 faster than 7.3?

The PHP language is constantly being developed by their creators and new versions are constantly being released. On November 28, 2019, a stable version of PHP 7.4 was released, which brings new features, improvements and performance improvements.

What is new in PHP 7.4?

What changes can we observe in the new PHP version compared to the previous ones?

Preloading – by using PHP and OPcache, which is installed by default on our servers, you can get better performance. The application code is first compiled and then saved into memory. This allows you to use the data that has been placed in the memory space and avoid recompiling the code

Arrow Functions – thanks to introduced in the latest PHP arrow functions version we can reduce the amount of code and increase code readability.

Code for PHP 7.3 and older versions:

array_map(function (User $user) { 
    return $user->id; 
}, $users)

The same code written with the new arrow functions used in PHP 7.4:

array_map(fn (User $user) => $user->id, $users)

Spread operator – PHP 7.4 also introduces the possibility of using the spread operator, so we can increase the performance of our application.

An example of using the spread operator:

$server= ['litespeed', 'redis', 'mariadb', 'php'];
$hosting= ['domains', 'administration', ...$server, 'support'];

PHP 7.4 performance

To check the performance of PHP 7.4 and the difference in loading time of websites based on this version and on the previous 7.3 we conducted tests. For this end, we used three different scripts. Each of them was performed ten times, and then we drew the average from the results, which we have included in the table below:

bench.phpmicro_bench.phpphp_benchmark.phpTotal time
PHP 7.40.322 s1.925 s0.727 s2.974 s
PHP 7.30.255 s1.696 s0.661 s2.612 s
Time difference (the less the better)0.067 s0.229 s0.066 s0.362 s
Percentage acceleration of PHP 7.4 compared to PHP 7.326%13%9%14%

The difference between the individual tests is clearly seen in the following charts:

Again, you can see that the latest version of PHP 7.4 is faster than the previous one. What is the situation with version 7.3, and even older (mostly unsupported) PHP versions? A year ago, we published the results on our blog at: We implement PHP 7.3 and compare it with other versions

A few words about the scripts used:

bench.php

This script mainly performs tasks in loops, in addition to simple functions (which in the loop perform variable value increment, calculate the length of a string or call an empty function) also performs, among others the following tasks:

  • calculates the Mandelbrot set,
  • Ackermann function,
  • calculates fibonacci sequence,
  • sorts the pile,
  • creates a multidimensional array.

Source: https://github.com/php/php-src/blob/master/Zend/bench.php

mcro_bench.php

This script is also based on loops and executes, among others the following tasks:

  • calls functions in the loop,
  • reads the value of the object’s property,
  • saves the value in the object’s property,
  • checks whether a value is assigned to the object property,
  • checks whether the object’s property value is not empty,
  • calls the object method,
  • reads the value of the static property of the object,
  • saves the value in the object’s static property,
  • check whether a value is assigned to the static property of the object,
  • checks whether the value of the object’s static property is not empty,
  • calls the object’s static method.

Source: https://github.com/php/php-src/blob/master/Zend/micro_bench.php

php_benchmark.php

The main task of this script is to perform standard operations on PHP and optionally on the database used by WordPress, Joomla, PrestaShop or other CMS:

  • calculation of mathematical operations,
  • string operations,
  • calling a loop,
  • operations on if else conditional statements.

Source: https://github.com/vanilla-php/benchmark-php/blob/master/benchmark.php

We encourage you to test your performance yourself and check for new PHP 7.4 features in Litespeed hosting. Each of our packages has the latest version set by default in PHP 7.4

Michael
Latest posts by Michael (see all)
Junior administrator and consultant at Smarthost.eu. A fan of heavy sounds. In addition to computer science and music, his life passion are mountains, to which he readily returns.

Leave a Reply