12

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?

Community
  • 1
  • 1
sathia
  • 2,192
  • 2
  • 24
  • 42
  • 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.... – James Butler Dec 06 '11 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 – sathia Dec 06 '11 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) – James Butler Dec 06 '11 at 14:30
  • @DaveRandom Not a dupe, because `array_search()` and `array_key_exists()` do different things :) – Ja͢ck Dec 28 '12 at 14:45
  • @Jack `array_search` didn't have any place to be in here from the start. – Eugene Dec 28 '12 at 14:47
  • @Eugene That's your opinion. The OP asked whether `array_search()` or `in_array()` based on numbered arrays. – Ja͢ck Dec 28 '12 at 14:48

2 Answers2

32

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Patrick Fisher
  • 7,926
  • 5
  • 35
  • 28
  • Do you have specific resources claiming that isset is substantially faster? – Francesco Pasa Jul 28 '16 at 07:02
  • 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: http://stackoverflow.com/questions/4518404/which-is-best-array-search-or-in-array/7683243#7683243 – Patrick Fisher Jul 29 '16 at 23:32
  • Ok, nice benchmark thanks! However I found the faster 'alternative' to be `issset()` by a significant margin (20-50%). See http://juliusbeckmann.de/blog/php-benchmark-isset-or-array_key_exists.html, http://www.php.net/manual/en/function.array-key-exists.php#82867 or http://www.zomeoff.com/php-fast-way-to-determine-a-key-elements-existance-in-an-array/. It should be noted however that isset() returns false if the key exists but the value is NULL. – Francesco Pasa Aug 05 '16 at 16:27
  • I found another benchmark which confirms: http://codepad.org/RcdAewVF – Francesco Pasa Aug 05 '16 at 16:35
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.

Community
  • 1
  • 1
lorenzo-s
  • 16,603
  • 15
  • 54
  • 86