I'm working on moving a site from PHP 7 to PHP 8. All seems to be going quite smoothly, but I've noticed that performance is slower on PHP 8. I'm not fussed about performance being massively better on 8 (although it would be nice), but I didn't expect it to be worse.
I thought some third-party dependency might be causing problems, so I did some testing with very basic code, and it's still significantly slower on PHP 8.
<?php
$perfTestStart = hrtime(true);
for($i = 0; $i < 10000000; $i++){
$random = rand(0,100);
}
$perfTestEnd = hrtime(true);
$perfTestDiff = $perfTestEnd - $perfTestStart;
ob_start();
echo 'PHP '.phpversion().': '.($perfTestDiff/1000000000).' seconds'."\r\n";
file_put_contents("c:\perftest.txt", ob_get_clean(), FILE_APPEND | LOCK_EX);
Here are the results from the file
PHP 7.4.33: 0.4066815 seconds
PHP 7.4.33: 0.4060634 seconds
PHP 7.4.33: 0.4058515 seconds
PHP 7.4.33: 0.4071766 seconds
PHP 7.4.33: 0.4058482 seconds
PHP 7.4.33: 0.4080825 seconds
PHP 7.4.33: 0.4066524 seconds
PHP 7.4.33: 0.4061863 seconds
PHP 7.4.33: 0.4054608 seconds
PHP 7.4.33: 0.4050715 seconds
PHP 8.4.13: 0.625234 seconds
PHP 8.4.13: 0.6220285 seconds
PHP 8.4.13: 0.6240802 seconds
PHP 8.4.13: 0.6250231 seconds
PHP 8.4.13: 0.6209979 seconds
PHP 8.4.13: 0.6227046 seconds
PHP 8.4.13: 0.6217932 seconds
PHP 8.4.13: 0.6242045 seconds
PHP 8.4.13: 0.6205148 seconds
In each case, I downloaded the Windows x64 Non Thread Safe version of PHP and unzipped it to a folder, and renamed php.ini-development to php.ini so the config is the default from the development ini file.
Why is performance significantly worse under PHP 8?
rand()was only moved fromext/standard/rand.ctoext/random/random.cstarting in PHP 8.2, but it still uses the same old MT19937 algorithm even in PHP 8.4.14.