Benchmark PHP 8.1 vs PHP 8.0 vs PHP 7.4 vs PHP 7.3 vs PHP 7.0 vs PHP 5.6 vs PHP 5.5
I have run benchmark PHP 8.1 vs PHP 8.0 which was released on November 25, 2021.
New features on PHP 8.1
- Enums
- Fibers
- Readonly Properties
- Intersection Types
- never return type
- final class constants
- New fsync and fdatasync functions
- New array_is_list function
- New Sodium XChaCha20 functions
- GD: AVIF image support
- Intl: New IntlDatePatternGenerator class
- Phar: Added OpenSSL-256 and OpenSSL-512 signature algorithms
- GD: Lossless WebP encoding support
- New #[ReturnTypeWillChange] attribute
- First-class Callable Syntax
- $_FILES: New full_path value for directory-uploads
- Array unpacking support for string-keyed arrays
- Explicit Octal numeral notation
- Hash functions accept algorithm-specific $options
- MurmurHash3 hash algorithm support
- xxHash hash algorithms support
- FPM: Configurable child-process spawn rate
- Curl: DNS-over-HTTPS support
- Curl: File uploads from strings with CURLStringFile
- MySQLi: New MYSQLI_REFRESH_REPLICA constant
The benchmarks I did are very simple, but represent what we use most of the time.
I chose to use a php script from php-benchmark-script.com and make some code adjustments because it doesn’t run on PHP 8.1.
Benchmark script run the following test:
- Test Math: 200.000
- Test String Manipulation: 200.000
- Test Loops: 20.000.000
- Test If Else If: 20.000.000
Below is the benchmark script:
<?php
ini_set('display_errors', 0);
/*
##########################################################################
# PHP Benchmark Performance Script #
# 2010 Code24 BV #
# #
# Author : Alessandro Torrisi #
# Company : Code24 BV, The Netherlands #
# Date : July 31, 2010 #
# version : 1.0 #
# License : Creative Commons CC-BY license #
# Website : http://www.php-benchmark-script.com #
# #
##########################################################################
*/
function test_Math($count = 200000) {
$time_start = microtime(true);
//$mathFunctions = array("abs", "acos", "asin", "atan", "bindec", "floor", "exp", "sin", "tan", "pi", "is_finite", "is_nan", "sqrt");
$mathFunctions = array("abs", "acos", "asin", "atan", "bindec", "floor", "exp", "sin", "tan", "is_finite", "is_nan", "sqrt");
foreach ($mathFunctions as $key => $function) {
if (!function_exists($function)) unset($mathFunctions[$key]);
}
for ($i=0; $i < $count; $i++) {
foreach ($mathFunctions as $function) {
$r = call_user_func_array($function, array($i));
}
}
return number_format(microtime(true) - $time_start, 3);
}
function test_StringManipulation($count = 200000) {
$time_start = microtime(true);
$stringFunctions = array("addslashes", "chunk_split", "metaphone", "strip_tags", "md5", "sha1", "strtoupper", "strtolower", "strrev", "strlen", "soundex", "ord");
foreach ($stringFunctions as $key => $function) {
if (!function_exists($function)) unset($stringFunctions[$key]);
}
$string = "the quick brown fox jumps over the lazy dog";
for ($i=0; $i < $count; $i++) {
foreach ($stringFunctions as $function) {
$r = call_user_func_array($function, array($string));
}
}
return number_format(microtime(true) - $time_start, 3);
}
function test_Loops($count = 20000000) {
$time_start = microtime(true);
for($i = 0; $i < $count; ++$i);
$i = 0; while($i < $count) ++$i;
return number_format(microtime(true) - $time_start, 3);
}
function test_IfElse($count = 20000000) {
$time_start = microtime(true);
for ($i=0; $i < $count; $i++) {
if ($i == -1) {
} elseif ($i == -2) {
} else if ($i == -3) {
}
}
return number_format(microtime(true) - $time_start, 3);
}
$total = 0;
$functions = get_defined_functions();
$line = str_pad("-",38,"-");
echo "$line\n|".str_pad("PHP BENCHMARK SCRIPT",36," ",STR_PAD_BOTH)."|\n$line\nStart : ".date("Y-m-d H:i:s")."\nPHP version : ".PHP_VERSION."\nPlatform : ".PHP_OS. "\n$line\n";
foreach ($functions['user'] as $user) {
if (preg_match('/^test_/', $user)) {
$total += $result = $user();
echo str_pad($user, 25) . " : " . $result ." sec.\n";
}
}
echo str_pad("-", 38, "-") . "\n" . str_pad("Total time:", 25) . " : " . $total ." sec.\n";
?>
The specification of my server is
- Operating System: CentOS Linux release 7.9.2009 (Core)
- CPU: Intel(R) Xeon(R) CPU E5-1650 v3 @ 3.50GHz 6 Core 12 Thread
- Memory: ECC 256GB
- Disk: SSD Enterprise
Below the results of Benchmark PHP 8.1 VS PHP 8.0, PHP 7.4, PHP 7.3, PHP 7.0, PHP 5.6 and PHP 5.5
Benchmark Result of PHP 8.1
Thanks a lot for you review, it’s really very useful!
You’re Welcome.
Thanks for sharing your results and testing methodologies for these benchmarks. Its quite amazing there is such a huge an incredible improvement in performance between even recent versions like 7.4.
I look forward to see your testing results for the latest versions in the near future.