Home » Php » Why php array gets slower with newer php versions, and how to go around that?

Why php array gets slower with newer php versions, and how to go around that?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have a php based system working perfectly since 2006, which has a backend (CLI, cron) process, processing millions of records, using a big tree-like structure in memory.

I’ve noticed great performance regressions when upgrading a debian machine.

So, to research the issue, I wrote a simple script, and downloaded a dozen xampp releases, and run the same script with the different php versions.

$start=microtime(true);
$n=10;
$counter=1;
$testarray=Array();

for ($i[1]=0;$i[1]<$n;$i[1]++) 
  for ($i[2]=0;$i[2]<$n;$i[2]++) 
    for ($i[3]=0;$i[3]<$n;$i[3]++) 
      for ($i[4]=0;$i[4]<$n;$i[4]++) 
        for ($i[5]=0;$i[5]<$n;$i[5]++) 
          for ($i[6]=0;$i[6]<$n;$i[6]++) 
            for ($i[7]=0;$i[7]<$n;$i[7]++) 
              $testarray[$i[1]][$i[2]][$i[3]][$i[4]][$i[5]][$i[6]][$i[7]]=$counter++;

$end=microtime(true);
echo "PHP ".phpversion()." ".round(memory_get_peak_usage(true)/(1024*1024),2)." mbyte max ram, ".round($end-$start,2)." seconds\r\n";

I run the same script against xampp win32 1.6.0a through win32 1.7.7-vc9, with the following results:

 - PHP 5.2.1 890.25 mbyte max ram, 12.43 seconds
 - PHP 5.2.1 890.25 mbyte max ram, 12.37 seconds
 - PHP 5.2.2 890.25 mbyte max ram, 12.43 seconds
 - PHP 5.2.3 890.25 mbyte max ram, 12.38 seconds
 - PHP 5.2.4 890.25 mbyte max ram, 12.5 seconds
 - PHP 5.2.5 890.25 mbyte max ram, 12.28 seconds
 - PHP 5.2.5 890.25 mbyte max ram, 12.31 seconds
 - PHP 5.2.6 890.25 mbyte max ram, 12.52 seconds
 - PHP 5.2.6 890.25 mbyte max ram, 12.54 seconds
 - PHP 5.2.8 890.25 mbyte max ram, 12.72 seconds
 - PHP 5.2.9 890.25 mbyte max ram, 12.53 seconds
 - PHP 5.3.0 975.5 mbyte max ram, 18.28 seconds
 - PHP 5.3.1 975.5 mbyte max ram, 18.06 seconds
 - PHP 5.3.5 975.5 mbyte max ram, 18.49 seconds
 - PHP 5.3.8 975.5 mbyte max ram, 17.97 seconds
 - PHP 5.3.8 975.5 mbyte max ram, 18.11 seconds

Notes:
– I don’t use xampp or windows on servers, but this was the easiest way to test through different versions at once.
– I tried to google for this problem a few times, but didn’t get any relevant results
– The speed loss is about the same (50% longer runtime) on the servers
– I don’t think that a slowdown of this kind is acceptable from any framework or compiler, – even in spite of new features the old ones should work just as well as before. I can’t tell my boss, that the same computer with the same software can’t do the same job anymore, because we dist-upgraded the debian on it… That would look like some windows upgrade. 🙂

Any ideas?

How to&Answers:

call gc_disable() before the loop. Cuts my time in half.

(php added a new garbage collector in 5.3)