Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hurie/cddf4b5804615b33f7665b9c647b9c9e to your computer and use it in GitHub Desktop.
Save hurie/cddf4b5804615b33f7665b9c647b9c9e to your computer and use it in GitHub Desktop.
in_array vs array_flip+isset vs array_search
<?php
ini_set('memory_limit', '2G');
$numGen = function () {
return rand(1, 9999999);
};
$rs = str_repeat("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 10);
$strGen = function () {
global $rs;
return substr(str_shuffle($rs), 0, rand(10, 100));
};
$br = PHP_EOL;
$round = 8;
$fmt = "%.{$round}f";
$loop = 4;
foreach (['$numGen' => $numGen, '$strGen' => $strGen] as $name => $gen) {
echo "\$gen={$name}", $br;
$s = $gen();
for ($b = 6; $b > 0; $b--) {
$c = 10 ** $b;
echo "N={$c}", $br;
$a = [];
for ($i = 0; $i < $c; $i++) {
$a[$i] = $gen();
}
$avg = [];
for ($i = 0; $i < $loop; $i++) {
echo ">>> loop={$i}", $br;
$t2 = microtime(1);
$a2 = array_flip($a);
$t2 = microtime(1) - $t2;
echo "flip: ", sprintf($fmt, round($t2, $round)), $br;
$t = microtime(1);
$e = in_array($s, $a);
$t = microtime(1) - $t;
echo "in_array: ", sprintf($fmt, round($t, $round)), $br;
$avg[][] = $t;
$t = microtime(1);
$e = isset($a2[$s]);
$t = microtime(1) - $t;
echo "flip+isset: ", sprintf($fmt, round($t + $t2, $round)), " (", sprintf($fmt, round($t, $round)), ")", $br;
$avg[][] = $t + $t2;
$avg[][] = $t;
$t = microtime(1);
$e = array_search($s, $a);
$t = microtime(1) - $t;
echo "array_search: ", sprintf($fmt, round($t, $round)), $br;
$avg[][] = $t;
$t = microtime(1);
$e = array_key_exists($s, $a2);
$t = microtime(1) - $t;
echo "flip+array_key_exists: ", sprintf($fmt, round($t + $t2, $round)), " (", sprintf($fmt, round($t, $round)), ")", $br;
$avg[][] = $t + $t2;
$avg[][] = $t;
}
echo ">>> loop=avg", $br;
echo "in_array: ", sprintf($fmt, round(array_sum($avg[0]) / count($avg[0]), $round)), $br;
echo "flip+isset: ", sprintf($fmt, round(array_sum($avg[1]) / count($avg[1]), $round)), " (", sprintf($fmt, round(array_sum($avg[2]) / count($avg[2]), $round)), ")", $br;
echo "array_search: ", sprintf($fmt, round(array_sum($avg[3]) / count($avg[3]), $round)), $br;
echo "flip+array_key_exists: ", sprintf($fmt, round(array_sum($avg[4]) / count($avg[4]), $round)), " (", sprintf($fmt, round(array_sum($avg[5]) / count($avg[5]), $round)), ")", $br;
echo $br;
}
echo $br;
echo str_repeat('-', 80), $br;
}
/*
-- php-7.1.27-nts-Win32-VC14-x64
$gen=$numGen
N=1000000
>>> loop=0
flip: 0.06617284
in_array: 0.00196600
flip+isset: 0.06617379 (0.00000095)
array_search: 0.00215411
flip+array_key_exists: 0.06617975 (0.00000691)
>>> loop=1
flip: 0.07074308
in_array: 0.00252414
flip+isset: 0.07074428 (0.00000119)
array_search: 0.00216508
flip+array_key_exists: 0.07074428 (0.00000119)
>>> loop=2
flip: 0.07250500
in_array: 0.00232315
flip+isset: 0.07250595 (0.00000095)
array_search: 0.00241303
flip+array_key_exists: 0.07250595 (0.00000095)
>>> loop=3
flip: 0.07291913
in_array: 0.00250101
flip+isset: 0.07292008 (0.00000095)
array_search: 0.00266886
flip+array_key_exists: 0.07292032 (0.00000119)
>>> loop=avg
in_array: 0.00196600
flip+isset: 0.06617379 (0.00000095)
array_search: 0.00215411
flip+array_key_exists: 0.06617975 (0.00000691)
N=100000
>>> loop=0
flip: 0.00717711
in_array: 0.00022984
flip+isset: 0.00717831 (0.00000119)
array_search: 0.00022888
flip+array_key_exists: 0.00717807 (0.00000095)
>>> loop=1
flip: 0.00315404
in_array: 0.00022602
flip+isset: 0.00315404 (0.00000000)
array_search: 0.00020099
flip+array_key_exists: 0.00315499 (0.00000095)
>>> loop=2
flip: 0.00322604
in_array: 0.00027704
flip+isset: 0.00322604 (0.00000000)
array_search: 0.00020194
flip+array_key_exists: 0.00322723 (0.00000119)
>>> loop=3
flip: 0.00303483
in_array: 0.00026107
flip+isset: 0.00303578 (0.00000095)
array_search: 0.00021100
flip+array_key_exists: 0.00303483 (0.00000000)
>>> loop=avg
in_array: 0.00022984
flip+isset: 0.00717831 (0.00000119)
array_search: 0.00022888
flip+array_key_exists: 0.00717807 (0.00000095)
N=10000
>>> loop=0
flip: 0.00043297
in_array: 0.00002193
flip+isset: 0.00043297 (0.00000000)
array_search: 0.00001311
flip+array_key_exists: 0.00043392 (0.00000095)
>>> loop=1
flip: 0.00021005
in_array: 0.00001001
flip+isset: 0.00021005 (0.00000000)
array_search: 0.00001311
flip+array_key_exists: 0.00021005 (0.00000000)
>>> loop=2
flip: 0.00014281
in_array: 0.00001121
flip+isset: 0.00014281 (0.00000000)
array_search: 0.00001311
flip+array_key_exists: 0.00014281 (0.00000000)
>>> loop=3
flip: 0.00012302
in_array: 0.00001097
flip+isset: 0.00012302 (0.00000000)
array_search: 0.00001287
flip+array_key_exists: 0.00012422 (0.00000119)
>>> loop=avg
in_array: 0.00002193
flip+isset: 0.00043297 (0.00000000)
array_search: 0.00001311
flip+array_key_exists: 0.00043392 (0.00000095)
N=1000
>>> loop=0
flip: 0.00002003
in_array: 0.00000191
flip+isset: 0.00002003 (0.00000000)
array_search: 0.00000215
flip+array_key_exists: 0.00002003 (0.00000000)
>>> loop=1
flip: 0.00001216
in_array: 0.00000191
flip+isset: 0.00001216 (0.00000000)
array_search: 0.00000191
flip+array_key_exists: 0.00001216 (0.00000000)
>>> loop=2
flip: 0.00001097
in_array: 0.00000095
flip+isset: 0.00001097 (0.00000000)
array_search: 0.00000095
flip+array_key_exists: 0.00001216 (0.00000119)
>>> loop=3
flip: 0.00001192
in_array: 0.00000095
flip+isset: 0.00001192 (0.00000000)
array_search: 0.00000119
flip+array_key_exists: 0.00001192 (0.00000000)
>>> loop=avg
in_array: 0.00000191
flip+isset: 0.00002003 (0.00000000)
array_search: 0.00000215
flip+array_key_exists: 0.00002003 (0.00000000)
N=100
>>> loop=0
flip: 0.00000191
in_array: 0.00000095
flip+isset: 0.00000191 (0.00000000)
array_search: 0.00000000
flip+array_key_exists: 0.00000191 (0.00000000)
>>> loop=1
flip: 0.00000191
in_array: 0.00000000
flip+isset: 0.00000191 (0.00000000)
array_search: 0.00000000
flip+array_key_exists: 0.00000286 (0.00000095)
>>> loop=2
flip: 0.00000095
in_array: 0.00000000
flip+isset: 0.00000095 (0.00000000)
array_search: 0.00000000
flip+array_key_exists: 0.00000095 (0.00000000)
>>> loop=3
flip: 0.00000119
in_array: 0.00000000
flip+isset: 0.00000119 (0.00000000)
array_search: 0.00000119
flip+array_key_exists: 0.00000119 (0.00000000)
>>> loop=avg
in_array: 0.00000095
flip+isset: 0.00000191 (0.00000000)
array_search: 0.00000000
flip+array_key_exists: 0.00000191 (0.00000000)
N=10
>>> loop=0
flip: 0.00000000
in_array: 0.00000000
flip+isset: 0.00000000 (0.00000000)
array_search: 0.00000000
flip+array_key_exists: 0.00000095 (0.00000095)
>>> loop=1
flip: 0.00000095
in_array: 0.00000000
flip+isset: 0.00000095 (0.00000000)
array_search: 0.00000095
flip+array_key_exists: 0.00000095 (0.00000000)
>>> loop=2
flip: 0.00000000
in_array: 0.00000095
flip+isset: 0.00000000 (0.00000000)
array_search: 0.00000000
flip+array_key_exists: 0.00000000 (0.00000000)
>>> loop=3
flip: 0.00000095
in_array: 0.00000095
flip+isset: 0.00000191 (0.00000095)
array_search: 0.00000119
flip+array_key_exists: 0.00000095 (0.00000000)
>>> loop=avg
in_array: 0.00000000
flip+isset: 0.00000000 (0.00000000)
array_search: 0.00000000
flip+array_key_exists: 0.00000095 (0.00000095)
--------------------------------------------------------------------------------
$gen=$strGen
N=1000000
>>> loop=0
flip: 0.16309714
in_array: 0.00638485
flip+isset: 0.16309810 (0.00000095)
array_search: 0.00628090
flip+array_key_exists: 0.16309834 (0.00000119)
>>> loop=1
flip: 0.09545803
in_array: 0.00760102
flip+isset: 0.09545898 (0.00000095)
array_search: 0.00744605
flip+array_key_exists: 0.09545922 (0.00000119)
>>> loop=2
flip: 0.09049296
in_array: 0.00677586
flip+isset: 0.09049392 (0.00000095)
array_search: 0.00619912
flip+array_key_exists: 0.09049392 (0.00000095)
>>> loop=3
flip: 0.09009409
in_array: 0.00729918
flip+isset: 0.09009409 (0.00000000)
array_search: 0.00621104
flip+array_key_exists: 0.09009528 (0.00000119)
>>> loop=avg
in_array: 0.00638485
flip+isset: 0.16309810 (0.00000095)
array_search: 0.00628090
flip+array_key_exists: 0.16309834 (0.00000119)
N=100000
>>> loop=0
flip: 0.02239108
in_array: 0.00080800
flip+isset: 0.02239203 (0.00000095)
array_search: 0.00076103
flip+array_key_exists: 0.02239203 (0.00000095)
>>> loop=1
flip: 0.00643802
in_array: 0.00071406
flip+isset: 0.00643897 (0.00000095)
array_search: 0.00059295
flip+array_key_exists: 0.00643897 (0.00000095)
>>> loop=2
flip: 0.00569701
in_array: 0.00073004
flip+isset: 0.00569701 (0.00000000)
array_search: 0.00059915
flip+array_key_exists: 0.00569820 (0.00000119)
>>> loop=3
flip: 0.00597787
in_array: 0.00070190
flip+isset: 0.00597787 (0.00000000)
array_search: 0.00060296
flip+array_key_exists: 0.00597882 (0.00000095)
>>> loop=avg
in_array: 0.00080800
flip+isset: 0.02239203 (0.00000095)
array_search: 0.00076103
flip+array_key_exists: 0.02239203 (0.00000095)
N=10000
>>> loop=0
flip: 0.00311017
in_array: 0.00014710
flip+isset: 0.00311112 (0.00000095)
array_search: 0.00007701
flip+array_key_exists: 0.00311112 (0.00000095)
>>> loop=1
flip: 0.00029302
in_array: 0.00002599
flip+isset: 0.00029302 (0.00000000)
array_search: 0.00002503
flip+array_key_exists: 0.00029302 (0.00000000)
>>> loop=2
flip: 0.00022912
in_array: 0.00002408
flip+isset: 0.00022912 (0.00000000)
array_search: 0.00002503
flip+array_key_exists: 0.00022912 (0.00000000)
>>> loop=3
flip: 0.00030613
in_array: 0.00004697
flip+isset: 0.00030613 (0.00000000)
array_search: 0.00003099
flip+array_key_exists: 0.00030732 (0.00000119)
>>> loop=avg
in_array: 0.00014710
flip+isset: 0.00311112 (0.00000095)
array_search: 0.00007701
flip+array_key_exists: 0.00311112 (0.00000095)
N=1000
>>> loop=0
flip: 0.00022411
in_array: 0.00000906
flip+isset: 0.00022507 (0.00000095)
array_search: 0.00000191
flip+array_key_exists: 0.00022411 (0.00000000)
>>> loop=1
flip: 0.00003409
in_array: 0.00000191
flip+isset: 0.00003409 (0.00000000)
array_search: 0.00000191
flip+array_key_exists: 0.00003409 (0.00000000)
>>> loop=2
flip: 0.00001788
in_array: 0.00000286
flip+isset: 0.00001788 (0.00000000)
array_search: 0.00000215
flip+array_key_exists: 0.00001884 (0.00000095)
>>> loop=3
flip: 0.00001717
in_array: 0.00000191
flip+isset: 0.00001717 (0.00000000)
array_search: 0.00000215
flip+array_key_exists: 0.00001717 (0.00000000)
>>> loop=avg
in_array: 0.00000906
flip+isset: 0.00022507 (0.00000095)
array_search: 0.00000191
flip+array_key_exists: 0.00022411 (0.00000000)
N=100
>>> loop=0
flip: 0.00001502
in_array: 0.00000000
flip+isset: 0.00001502 (0.00000000)
array_search: 0.00000095
flip+array_key_exists: 0.00001621 (0.00000119)
>>> loop=1
flip: 0.00000405
in_array: 0.00000000
flip+isset: 0.00000405 (0.00000000)
array_search: 0.00000000
flip+array_key_exists: 0.00000501 (0.00000095)
>>> loop=2
flip: 0.00000191
in_array: 0.00000000
flip+isset: 0.00000191 (0.00000000)
array_search: 0.00000000
flip+array_key_exists: 0.00000191 (0.00000000)
>>> loop=3
flip: 0.00000095
in_array: 0.00000000
flip+isset: 0.00000095 (0.00000000)
array_search: 0.00000095
flip+array_key_exists: 0.00000095 (0.00000000)
>>> loop=avg
in_array: 0.00000000
flip+isset: 0.00001502 (0.00000000)
array_search: 0.00000095
flip+array_key_exists: 0.00001621 (0.00000119)
N=10
>>> loop=0
flip: 0.00000119
in_array: 0.00000119
flip+isset: 0.00000119 (0.00000000)
array_search: 0.00000000
flip+array_key_exists: 0.00000238 (0.00000119)
>>> loop=1
flip: 0.00000000
in_array: 0.00000000
flip+isset: 0.00000000 (0.00000000)
array_search: 0.00000000
flip+array_key_exists: 0.00000119 (0.00000119)
>>> loop=2
flip: 0.00000095
in_array: 0.00000000
flip+isset: 0.00000095 (0.00000000)
array_search: 0.00000000
flip+array_key_exists: 0.00000095 (0.00000000)
>>> loop=3
flip: 0.00000095
in_array: 0.00000000
flip+isset: 0.00000095 (0.00000000)
array_search: 0.00000000
flip+array_key_exists: 0.00000095 (0.00000000)
>>> loop=avg
in_array: 0.00000119
flip+isset: 0.00000119 (0.00000000)
array_search: 0.00000000
flip+array_key_exists: 0.00000238 (0.00000119)
--------------------------------------------------------------------------------
Process finished with exit code 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment