Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KennFatt/89e90d3d9f51b734c4319c8b473527ab to your computer and use it in GitHub Desktop.
Save KennFatt/89e90d3d9f51b734c4319c8b473527ab to your computer and use it in GitHub Desktop.
in_array vs array_flip+isset vs array_search
<?php
$a = [];
//$s = 123456;
$s = 's6tbdfgj222dJGk';
$rs = str_repeat("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 10);
$numGen = function() {
return rand(1, 9999999);
};
$strGen = function() {
global $rs;
return substr(str_shuffle($rs), 0, rand(10, 100));
};
//$gen = $numGen;
$gen = $strGen;
foreach ([10000, 1000, 100, 10] as $c) {
echo "N={$c}\n";
for ($i = 0; $i < $c; $i++) {
$a[$i] = $gen();
// if ($a[$i] === $s) { $a[$i] = null; }
// if ($i === 0) { $a[$i] = $s; }
// if ($i === $c / 2) { $a[$i] = $s; }
// if ($i === $c - 1) { $a[$i] = $s; }
}
$t = microtime(1);
$e = in_array($s, $a);
echo "in_array: ", microtime(1) - $t, PHP_EOL;
$t = microtime(1);
$a = array_flip($a);
$e = isset($a[$s]);
echo "flip+isset: ", microtime(1) - $t, PHP_EOL;
$t = microtime(1);
$e = array_search($s, $a);
echo "array_search: ", microtime(1) - $t, PHP_EOL;
echo PHP_EOL;
}
/*
1 Numbers
1.1 Random
N=10000
in_array: 0.00029611587524414
flip+isset: 0.0032370090484619
array_search: 0.00028896331787109
N=1000
in_array: 0.00029897689819336
flip+isset: 0.0026841163635254
array_search: 0.00031399726867676
N=100
in_array: 0.00026202201843262
flip+isset: 0.0023770332336426
array_search: 0.00028109550476074
N=10
in_array: 0.00024914741516113
flip+isset: 0.0020699501037598
array_search: 0.00026917457580566
1.2 Wanted element is absent
N=10000
in_array: 0.00027894973754883
flip+isset: 0.0030219554901123
array_search: 0.00030684471130371
N=1000
in_array: 0.00028204917907715
flip+isset: 0.0025548934936523
array_search: 0.0003049373626709
N=100
in_array: 0.0002751350402832
flip+isset: 0.002277135848999
array_search: 0.00025510787963867
N=10
in_array: 0.00024795532226562
flip+isset: 0.0019938945770264
array_search: 0.00026082992553711
1.3 Wanted element is in the beginning
N=10000
in_array: 2.3126602172852E-5
flip+isset: 0.0031042098999023
array_search: 0.00029897689819336
N=1000
in_array: 0.00023198127746582
flip+isset: 0.0025599002838135
array_search: 5.9604644775391E-6
N=100
in_array: 6.9141387939453E-6
flip+isset: 0.0022938251495361
array_search: 0.00023508071899414
N=10
in_array: 0.00024080276489258
flip+isset: 0.0020699501037598
array_search: 5.0067901611328E-6
1.4 Wanted element is in the middle
N=10000
in_array: 0.00016307830810547
flip+isset: 0.0030930042266846
array_search: 0.00028681755065918
N=1000
in_array: 0.0002598762512207
flip+isset: 0.0025320053100586
array_search: 0.0001368522644043
N=100
in_array: 9.0599060058594E-6
flip+isset: 0.0024280548095703
array_search: 0.00029802322387695
N=10
in_array: 0.00023889541625977
flip+isset: 0.0021507740020752
array_search: 5.9604644775391E-6
1.5 Wanted element is in the end
N=10000
in_array: 0.00029397010803223
flip+isset: 0.0031838417053223
array_search: 0.00027799606323242
N=1000
in_array: 0.00025105476379395
flip+isset: 0.0025491714477539
array_search: 0.00028681755065918
N=100
in_array: 9.0599060058594E-6
flip+isset: 0.0023031234741211
array_search: 0.00025701522827148
N=10
in_array: 0.00022506713867188
flip+isset: 0.0020210742950439
array_search: 8.1062316894531E-6
2 Strings
2.1 Random
N=10000
in_array: 0.00095987319946289
flip+isset: 0.0060811042785645
array_search: 6.9141387939453E-6
N=1000
in_array: 7.8678131103516E-6
flip+isset: 0.0040390491485596
array_search: 0.0012731552124023
N=100
in_array: 0.00082921981811523
flip+isset: 0.0047550201416016
array_search: 6.9141387939453E-6
N=10
in_array: 5.9604644775391E-6
flip+isset: 0.0033798217773438
array_search: 0.0010240077972412
2.2 Wanted element is absent
N=10000
in_array: 0.0011100769042969
flip+isset: 0.0058979988098145
array_search: 9.0599060058594E-6
N=1000
in_array: 8.1062316894531E-6
flip+isset: 0.0041060447692871
array_search: 0.001276969909668
N=100
in_array: 0.000823974609375
flip+isset: 0.0054380893707275
array_search: 6.9141387939453E-6
N=10
in_array: 5.9604644775391E-6
flip+isset: 0.0036108493804932
array_search: 0.0011579990386963
2.3 Wanted element is in the beginning
N=10000
in_array: 3.2186508178711E-5
flip+isset: 0.005850076675415
array_search: 8.1062316894531E-6
N=1000
in_array: 7.8678131103516E-6
flip+isset: 0.0038559436798096
array_search: 7.1525573730469E-6
N=100
in_array: 6.9141387939453E-6
flip+isset: 0.0050489902496338
array_search: 5.0067901611328E-6
N=10
in_array: 7.1525573730469E-6
flip+isset: 0.0034289360046387
array_search: 5.9604644775391E-6
2.4 Wanted element is in the middle
N=10000
in_array: 0.00057315826416016
flip+isset: 0.0059258937835693
array_search: 8.1062316894531E-6
N=1000
in_array: 8.1062316894531E-6
flip+isset: 0.0039551258087158
array_search: 0.00060606002807617
N=100
in_array: 1.0967254638672E-5
flip+isset: 0.0049099922180176
array_search: 6.9141387939453E-6
N=10
in_array: 5.9604644775391E-6
flip+isset: 0.0035531520843506
array_search: 1.5020370483398E-5
2.5 Wanted element is in the end
N=10000
in_array: 0.0011200904846191
flip+isset: 0.0057530403137207
array_search: 8.1062316894531E-6
N=1000
in_array: 9.0599060058594E-6
flip+isset: 0.0038230419158936
array_search: 0.0011618137359619
N=100
in_array: 1.5974044799805E-5
flip+isset: 0.0047528743743896
array_search: 6.9141387939453E-6
N=10
in_array: 6.9141387939453E-6
flip+isset: 0.0034749507904053
array_search: 2.0027160644531E-5
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment