Skip to content

Instantly share code, notes, and snippets.

@ksimka
Created March 4, 2015 13:24
Show Gist options
  • Save ksimka/21a6ff74b41451c430e8 to your computer and use it in GitHub Desktop.
Save ksimka/21a6ff74b41451c430e8 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
*/
@SamMousa
Copy link

However if you are checking for multiple elements wouldn't these results skew towards using array_flip + isset?

Since for any number of lookups you can do 1 array_flip and then benefit from the constant time of isset (or array_key_exists)

@zyberspace
Copy link

So this discussion is a couple of months old, but as i just needed this:

If you need to check for more than 10 elements array_flip + isset is indeed faster. @SamMousa

Searching for an element once

N=10000
in_array:     0.0003502368927002
flip+isset:   0.0049781799316406

Searching for an element 10 times

N=10000
in_array:     0.0056309700012207
flip+isset:   0.0052509307861328

Searching for an element 20 times

N=10000
in_array:     0.014267921447754
flip+isset:   0.0051031112670898

The modified code i used

$tries = 20;

$t = microtime(1);
for ($i=1; $i <= $tries; $i++) {
    $e = in_array($s, $a);
}
echo "in_array:     ", microtime(1) - $t, PHP_EOL;
$t = microtime(1);
$a = array_flip($a);
for ($i=1; $i <= $tries; $i++) {
    $e = isset($a[$s]);
}
echo "flip+isset:   ", microtime(1) - $t, PHP_EOL;

@hrvolapeter
Copy link

hrvolapeter commented May 10, 2018

10000 is really small sample, you need to go about 8M+ for real comparison. flip+isset is much faster for large arrays then other search methods if you do multiple searches.

@qqqrppp
Copy link

qqqrppp commented Dec 2, 2018

PHP 7.2.10-0

$a = [];
$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));
};

$br = PHP_EOL;
$round = 8;

$gen = $strGen;
foreach ([10000, 1000, 100, 10] as $c) {
	echo "N={$c}", $br;
	
	for ($i = 0; $i < $c; $i++) {
	    $a[$i] = $gen();
	}

	$t = microtime(1);
	$e = in_array($s, $a);
	echo "in_array: ", round(microtime(1) - $t, $round), $br;

	$t = microtime(1);
	$a = array_flip($a);
	$e = isset($a[$s]);
	echo "flip+isset: ", round(microtime(1) - $t, $round), $br;

	$t = microtime(1);
	$e = array_search($s, $a);
	echo "array_search: ", round(microtime(1) - $t, $round), $br;

	$t = microtime(1);
	$a = array_flip($a);
	$e = array_key_exists($s, $a);
	echo "flip+array_key_exists: ", round(microtime(1) - $t, $round), $br;

	echo $br;
}

N=10000
in_array: 5.603E-5
flip+isset: 0.0015831
array_search: 8.82E-6
flip+array_key_exists: 0.00017786

N=1000
in_array: 4.888E-5
flip+isset: 0.00038695
array_search: 5.01E-6
flip+array_key_exists: 0.00017405

N=100
in_array: 3.91E-5
flip+isset: 0.00031304
array_search: 5.96E-6
flip+array_key_exists: 0.0001812

N=10
in_array: 4.005E-5
flip+isset: 0.00030804
array_search: 4.05E-6
flip+array_key_exists: 0.00017905

@hurie
Copy link

hurie commented May 16, 2019

Sorry, but did $a use the the biggest $c (e.g 10000) for every $c since there no reset for $a. And did $a flipped when doing array_search.

@aurovrata
Copy link

php 7.3.11

testing array_flip separately,

for string searches, flip (once) + isset becomes faster for more than 100 to 200 searches.

for numerical searches, flip (once) + isset becomes faster above 10 searches.

  1. results for String search

N=1000000 (million)
in_array: 0.00845003
flip: 0.17343211
isset: 2.86E-6
array_search: 0.00835395
array_key_exists: 5.01E-6

N=100000
in_array: 0.00854707
flip: 0.12469196
isset: 7.15E-6
array_search: 0.00861216
array_key_exists: 6.2E-6

N=10000
in_array: 0.00854087
flip: 0.10549212
isset: 6.91E-6
array_search: 0.00846505
array_key_exists: 4.05E-6

  1. Numerical search results,

N=1000000
in_array: 0.01197696
flip: 0.06217289
isset: 6.2E-6
array_search: 0.01673698
array_key_exists: 4.05E-6

N=100000
in_array: 0.01191092
flip: 0.06582093
isset: 6.91E-6
array_search: 0.01637983
array_key_exists: 4.05E-6

N=10000
in_array: 0.01375008
flip: 0.07185006
isset: 5.01E-6
array_search: 0.01485705
array_key_exists: 4.05E-6

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