0

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?

4
  • 1
    As far as I know php for windows has always been a second class citizen and not very optimized or performant Commented Oct 22 at 15:15
  • Security holes were fixed, which needed more performance. You don't want your webserver being hacked and work as a crypto farm, do you? Commented Oct 22 at 20:50
  • 1
    At first, I thought PHP had switched from the Mersenne Twister (MT19937) algorithm to the PCG32 (Permuted Congruential Generator) algorithm. While PCG32 itself is lighter and statistically better than Mersenne Twister, I assumed PHP 8’s implementation added extra layers for thread-safety, bias correction, and range validation, which would explain the performance difference. However, it turns out that rand() was only moved from ext/standard/rand.c to ext/random/random.c starting in PHP 8.2, but it still uses the same old MT19937 algorithm even in PHP 8.4.14. Commented Oct 23 at 5:30
  • 1
    It’s great that the PHP community continues to provide Windows binaries even after Microsoft ended official support, both for on-premises Windows Server and for Azure App Service (Windows). That said, PHP users on Windows should be aware that things have changed: platform-level optimization and tuning can be missing. If performance matters, it’s generally better to run PHP on Linux, where most of the active development and optimization resources have been concentrated for decades. Commented Oct 27 at 0:48

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.