Skip to content

Instantly share code, notes, and snippets.

@andrey-legayev
Created October 7, 2019 08:44
Show Gist options
  • Save andrey-legayev/079652e03b9fd5d87740c706ad91035a to your computer and use it in GitHub Desktop.
Save andrey-legayev/079652e03b9fd5d87740c706ad91035a to your computer and use it in GitHub Desktop.
PHP benchmark: array_flip() performance dependent on array size
<?php
function test($size)
{
// prepare arrays
$plain = [];
for ($i = 0; $i < $size; $i++) {
$value = rand();
array_push($plain, $value);
}
// run tests
$t1 = microtime(true);
$x = array_flip($plain);
$t1 = microtime(true) - $t1;
// echo results as CSV
printf("%s, %d, %f\n", phpversion(), $size, $t1);
}
echo "# PHP Version, Array Size, array_flip()\n";
for ($i = 1; $i <= 100000; $i = $i * 10) {
test($i);
}
#!/bin/bash
for ver in 7.0 7.1 7.2 7.3; do
echo "# running tests for version $ver"
image=php:$ver-cli
docker run --rm -v "$PWD":/src $image \
bash -c "php /src/perf-test.php"
done
@andrey-legayev
Copy link
Author

My results:
image

X Y Scatter Plot:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment