PHP performance increase from 5.6 to 7.2

I remember when PHP 7 was released. The first thing I did was a simple performance test. I don’t remember the script exactly, but it was similar to this:

<?php

$time = microtime(true);

$array = [];
for ($i = 0; $i < 10000; $i++) {
    if (!array_key_exists($i, $array)) {
        $array[$i] = [];
    }
    
    for ($j = 0; $j < 1000; $j++) {
        if (!array_key_exists($j, $array[$i])) {
            $array[$i][$j] = true;
        }
    }
}

echo sprintf(
    "Execution time: %f seconds\nMemory usage: %f MB\n\n",
    microtime(true) - $time,
    memory_get_usage(true) / 1024 / 1024
);

The results made me really happy. Memory usage was much lower and time execution had decreased a lot.

Recently, with the above script saved as “test.php”, I ran the test again, this time for multiple PHP version.

#!/usr/bin/env bash

versions=( 5.6 7.0 7.1 7.2 )

for v in "${versions[@]}"
do
    cmd="docker run --rm -ti -v ${PWD}/test.php:/test.php php:${v}-alpine3.7 php -d memory_limit=2048M test.php"
    sh -c "echo ${v} && ${cmd}"
done

With the results:

5.6
Execution time: 4.288818 seconds
Memory usage: 1379.250000 MB

7.0
Execution time: 1.433924 seconds
Memory usage: 360.000000 MB

7.1
Execution time: 1.376682 seconds
Memory usage: 360.000000 MB

7.2
Execution time: 0.590186 seconds
Memory usage: 360.000000 MB

4 thoughts on “PHP performance increase from 5.6 to 7.2”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.