14

Possible Duplicate:
What’s quicker and better to determine if an array key exists in PHP?

suppose I want to store the list of friends I have on memcache. sometimes i need to search if a user is on my list and sometime i need to fetch all the list of friends.

would you rather

$friends[] = $friend

or

$friends[$friend] = 1;

the rationale is to save as much as ram as decently possible without penalizing speed. I didn't find any case study for php 5.3.8 that can help me on my little dilemma: under load, which is faster to perform?

array_key_exists or in_array? (ie: is foo a friend of bar?)

Also, sometimes i need to fetch the whole list of friends so i need to iterate the whole list in order to build an array of friends. not sure at all about the second method, since I don't know yet if there will be more array_search|array_key_exists|in_array or fetch of full friends list.

any idea?

6
  • 2
    Have you tried making a benchmark script yourself and testing it? Also this a duplicate of just about every other php array speed question on SO.... Commented Dec 6, 2011 at 14:01
  • 1
    Not yet, I can of course make some benchmarks on this. I was curious to know if someone actually can argument what the good practice here should be. I'll post the benchmarks Commented Dec 6, 2011 at 14:04
  • 1
    Good practice is largely dictated to by the situation in which you find yourself. I've been burnt before from following (what worked) for someone else just because my environmental factors differed. (+its amazing what you find out when you do stuff yourself) Commented Dec 6, 2011 at 14:30
  • @DaveRandom Not a dupe, because array_search() and array_key_exists() do different things :) Commented Dec 28, 2012 at 14:45
  • @Jack array_search didn't have any place to be in here from the start. Commented Dec 28, 2012 at 14:47

2 Answers 2

35

array_key_exists is much faster. array_search must traverse the whole array, so it is O(n). array_key_exists is a hash table lookup, so it is O(1).

See http://en.wikipedia.org/wiki/Big_O_notation if you are new to this concept.

Between array_key_exists and isset, though both are very fast [O(1)], isset is significantly faster. If this check is happening many thousands of times, you'd want to use isset.

It should be noted that they are not identical, though -- when the array key exists but the value is null, isset will return false and array_key_exists will return true. If the value may be null, you need to use array_key_exists.

Sign up to request clarification or add additional context in comments.

4 Comments

Do you have specific resources claiming that isset is substantially faster?
I made this change in a large codebase and measured an improvement at the time. I suggest you run a quick test, yourself. See my sample benchmark script here: stackoverflow.com/questions/4518404/…
Ok, nice benchmark thanks! However I found the faster 'alternative' to be issset() by a significant margin (20-50%). See juliusbeckmann.de/blog/…, php.net/manual/en/function.array-key-exists.php#82867 or zomeoff.com/…. It should be noted however that isset() returns false if the key exists but the value is NULL.
I found another benchmark which confirms: codepad.org/RcdAewVF
2

You can run a simple test by yourself. Anyway, if $friends should contains unique elements (no duplicate values!!), you can use keys to store them.

I think it's faster for PHP to check for keys (array_key_exists() or simply isset($array[$key])). To search for a value, PHP must cycle through the array; to search for a key PHP will use a hash function.

You can read more on stackoverflow.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.