Fastest way to check for value existence
I have a list of values I have to check my input against it for existence.
What is the fastest way?
This is really out of curiosity on how the in开发者_如何转开发ternals work, not any stuff about premature optimization etc...
1.
$x=array('v'=>'','c'=>'','w'=>);
..
..
array_key_exists($input,$x);
2.
$x=array('v','c','w');
..
..
in_array($input,$x);
How about isset($x[$input])
which, if suitable for your needs, would generally beat both of those presented.
Of the two methods in the question, array_key_exists
has less work to do than in_array
so if you had to choose between only those two then array_key_exists
would be it.
Aside: Do you have any specific questions about "the internals"?
in my experience, array_key_exists is faster 99% of the time, especially as the array size grows.
that being said, isset is even faster, as it does a hash lookup vs an array value search, though isset will return false on blank values, as shown in your example array.
精彩评论