开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜