Skip to content

Instantly share code, notes, and snippets.

@yowcow
Created May 11, 2018 06:20
Show Gist options
  • Save yowcow/29beb367cc83cc9024a51e3e24234b6c to your computer and use it in GitHub Desktop.
Save yowcow/29beb367cc83cc9024a51e3e24234b6c to your computer and use it in GitHub Desktop.
Flat array or assoc array
<?php
$try_count = 50000000;
$t0 = microtime(true);
for ($i = 0; $i < $try_count; $i++) {
$data1 = [
'EU', 'AD', 'AL', 'AT', 'BA', 'BE', 'BG', 'BY',
'CH', 'CS', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI',
'FO', 'FR', 'FX', 'GB', 'GI', 'GR', 'HR', 'HU',
'IE', 'IS', 'IT', 'LI', 'LT', 'LU', 'LV', 'MC',
'MD', 'MK', 'MT', 'NL', 'NO', 'PL', 'PT', 'RO',
'SE', 'SI', 'SJ', 'SK', 'SM', 'UA', 'VA'
];
}
$t1 = microtime(true);
for ($i = 0; $i < $try_count; $i++) {
$data2 = [
'EU' => true,
'AD' => true,
'AL' => true,
'AT' => true,
'BA' => true,
'BE' => true,
'BG' => true,
'BY' => true,
'CH' => true,
'CS' => true,
'CZ' => true,
'DE' => true,
'DK' => true,
'EE' => true,
'ES' => true,
'FI' => true,
'FO' => true,
'FR' => true,
'FX' => true,
'GB' => true,
'GI' => true,
'GR' => true,
'HR' => true,
'HU' => true,
'IE' => true,
'IS' => true,
'IT' => true,
'LI' => true,
'LT' => true,
'LU' => true,
'LV' => true,
'MC' => true,
'MD' => true,
'MK' => true,
'MT' => true,
'NL' => true,
'NO' => true,
'PL' => true,
'PT' => true,
'RO' => true,
'SE' => true,
'SI' => true,
'SJ' => true,
'SK' => true,
'SM' => true,
'UA' => true,
'VA' => true,
];
}
$t2 = microtime(true);
$create_simple_array = $t1 - $t0;
$create_assoc_array = $t2 - $t1;
echo "--> Array declaration after {$try_count} times:\n";
printf("Simple array: %f sec\n", $create_simple_array);
printf("Assoc array: %f sec\n", $create_assoc_array);
$data1 = [
'EU', 'AD', 'AL', 'AT', 'BA', 'BE', 'BG', 'BY',
'CH', 'CS', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI',
'FO', 'FR', 'FX', 'GB', 'GI', 'GR', 'HR', 'HU',
'IE', 'IS', 'IT', 'LI', 'LT', 'LU', 'LV', 'MC',
'MD', 'MK', 'MT', 'NL', 'NO', 'PL', 'PT', 'RO',
'SE', 'SI', 'SJ', 'SK', 'SM', 'UA', 'VA'
];
$data2 = [
'EU' => true,
'AD' => true,
'AL' => true,
'AT' => true,
'BA' => true,
'BE' => true,
'BG' => true,
'BY' => true,
'CH' => true,
'CS' => true,
'CZ' => true,
'DE' => true,
'DK' => true,
'EE' => true,
'ES' => true,
'FI' => true,
'FO' => true,
'FR' => true,
'FX' => true,
'GB' => true,
'GI' => true,
'GR' => true,
'HR' => true,
'HU' => true,
'IE' => true,
'IS' => true,
'IT' => true,
'LI' => true,
'LT' => true,
'LU' => true,
'LV' => true,
'MC' => true,
'MD' => true,
'MK' => true,
'MT' => true,
'NL' => true,
'NO' => true,
'PL' => true,
'PT' => true,
'RO' => true,
'SE' => true,
'SI' => true,
'SJ' => true,
'SK' => true,
'SM' => true,
'UA' => true,
'VA' => true,
];
$t0 = microtime(true);
for ($i = 0; $i < $try_count; $i++) {
in_array('hoge', $data1);
}
$t1 = microtime(true);
for ($i = 0; $i < $try_count; $i++) {
array_key_exists('hoge', $data2);
}
$t2 = microtime(true);
$search_simple_array = $t1 - $t0;
$search_assoc_array = $t2 - $t1;
echo "--> Array key search after {$try_count} times:\n";
printf("Simple array: %f sec\n", $search_simple_array);
printf("Assoc array: %f sec\n", $search_assoc_array);
echo "--> Total after {$try_count} times:\n";
printf("Simple array: %f sec\n", $create_simple_array + $search_simple_array);
printf("Assoc array: %f sec\n", $create_assoc_array + $search_assoc_array);
/*
--> Array declaration after 50000000 times:
Simple array: 10.235229 sec
Assoc array: 12.065151 sec
--> Array key search after 50000000 times:
Simple array: 3.945283 sec
Assoc array: 1.502123 sec
--> Total after 50000000 times:
Simple array: 14.180512 sec
Assoc array: 13.567274 sec
*/
@yowcow
Copy link
Author

yowcow commented May 11, 2018

% php -v
PHP 7.0.28-0ubuntu0.16.04.1 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.28-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies

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