algorithm - PHP Most efficient way to find a number in a list -


i have list of 300 numbers, not ordered. find efficient way determine if number in list.

the simple answer in_array(). of course has big o of o(n).

but since have complete control on list, believed can make faster.

so made list associative array key number looking isset() yield me o(1).

$myarray = array('434342'=>true, '345235'=>true, '562211'=>true, '3333245'=>true, '99087782'=>true); 

is best way? since array size small hit of o(n) n=300 not worth effort.

300 array elements:

in_array()     0.00000000000000000000 seconds     bool(true)  isset()     0.00000000000000000000 seconds     bool(true) 

500,000 array elements:

in_array()     0.00500106811523437500 seconds     bool(true)  isset()     0.00000000000000000000 seconds     bool(true) 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -