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
[root@serverdiary share]# /usr/bin/php bench.php
--------------------------------------
| PHP BENCHMARK SCRIPT |
--------------------------------------
Start : 2021-12-02 16:48:35
PHP version : 8.1.0
Platform : Linux
--------------------------------------
test_math : 0.320 sec.
test_stringmanipulation : 0.353 sec.
test_loops : 0.016 sec.
test_ifelse : 0.008 sec.
--------------------------------------
Total time: : 0.697 sec.
Benchmark Result of PHP 8.0
[root@serverdiary share]# /opt/remi/php80/root/usr/bin/php bench.php
--------------------------------------
| PHP BENCHMARK SCRIPT |
--------------------------------------
Start : 2021-12-02 16:48:41
PHP version : 8.0.13
Platform : Linux
--------------------------------------
test_math : 0.316 sec.
test_stringmanipulation : 0.380 sec.
test_loops : 0.134 sec.
test_ifelse : 0.225 sec.
--------------------------------------
Total time: : 1.055 sec.
Benchmark Result of PHP 7.4
[root@serverdiary share]# /opt/remi/php74/root/usr/bin/php bench.php
--------------------------------------
| PHP BENCHMARK SCRIPT |
--------------------------------------
Start : 2021-12-02 16:48:50
PHP version : 7.4.26
Platform : Linux
--------------------------------------
test_math : 0.318 sec.
test_stringmanipulation : 0.414 sec.
test_loops : 0.129 sec.
test_ifelse : 0.278 sec.
--------------------------------------
Total time: : 1.139 sec.
Benchmark Result of PHP 7.3
[root@serverdiary share]# /opt/remi/php73/root/usr/bin/php bench.php
--------------------------------------
| PHP BENCHMARK SCRIPT |
--------------------------------------
Start : 2021-12-02 16:48:59
PHP version : 7.3.33
Platform : Linux
--------------------------------------
test_math : 0.266 sec.
test_stringmanipulation : 0.372 sec.
test_loops : 0.203 sec.
test_ifelse : 0.301 sec.
--------------------------------------
Total time: : 1.142 sec.
Benchmark Result of PHP 7.0
[root@serverdiary share]# /opt/remi/php70/root/usr/bin/php bench.php
--------------------------------------
| PHP BENCHMARK SCRIPT |
--------------------------------------
Start : 2021-12-02 16:50:26
PHP version : 7.0.33
Platform : Linux
--------------------------------------
test_math : 0.338 sec.
test_stringmanipulation : 0.492 sec.
test_loops : 0.224 sec.
test_ifelse : 0.318 sec.
--------------------------------------
Total time: : 1.372 sec.
Benchmark Result of PHP 5.6
[root@serverdiary share]# /opt/remi/php56/root/usr/bin/php bench.php
--------------------------------------
| PHP BENCHMARK SCRIPT |
--------------------------------------
Start : 2021-12-04 14:32:57
PHP version : 5.6.40
Platform : Linux
--------------------------------------
test_math : 1.167 sec.
test_stringmanipulation : 1.239 sec.
test_loops : 0.625 sec.
test_ifelse : 0.860 sec.
--------------------------------------
Total time: : 3.891 sec.
Benchmark Result of PHP 5.5
[root@serverdiary share]# /opt/remi/php55/root/usr/bin/php bench.php
--------------------------------------
| PHP BENCHMARK SCRIPT |
--------------------------------------
Start : 2021-12-04 14:33:10
PHP version : 5.5.38
Platform : Linux
--------------------------------------
test_math : 1.176 sec.
test_stringmanipulation : 1.279 sec.
test_loops : 0.652 sec.
test_ifelse : 0.903 sec.
--------------------------------------
Total time: : 4.01 sec.
Conclusion: PHP 8.1 greatly improved, about 34% faster than PHP 8.0, 39% faster than PHP 7.4, 49% faster than php 7.0, 82% faster than PHP 5.6 and 82% faster than PHP 5.5.
So with PHP 8.1 I can handle more client. And PHP 8.1 can lower your TTBF (Time To First Byte), and improve your SEO.
Also read: How to Check TTFB (Time To First Byte) using CURL
Time to first byte (TTBF) is one of the many reasons why a website is fast or slow.
Now, Google use website speed as SEO ranking factor. Google tells that users won’t wait slow site.
Thanks a lot for you review, it’s really very useful!
You’re Welcome.