Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Performance question: Faster to leave duplicates in array that will be searched or do array_unique?

I have code that adds values to an array. The array is later searched in another part of my code. The values that are added to the array are not necessarily unique, so it's possible to end up with duplicate values in the array being searched. Technically speaking, even with the duplicates present in the array being searched, my code works fine and I'll be able to find the value. I just want to know if the value is in the array being searched, and don't care if it's in the array 1 time or 10,000 times.

My question is whether it's preferred (for performance and/or style reasons) to do array_unique() on my array being searched before I do the search.

So for example, suppose I want to search an array like this:

$searchMe = Array("dog", "cat", "mouse", "dog", "dog", "dog");

Note that "dog" is present 4 times. If I want to search for the value "dog", in that array, it will work fine and I will be able to tell that it's present. As mentioned above, I don't care how many times it's present, I just want to know if it's present at all.

So should I do this first before searching and then search against the de-duped array?

$searchMe_cleaned = array_unique($searchMe);

I.e., will that be faster than just searching the array with the duplicates?

Please keep in mind that although in this example the array being searched just has a few elements, the real array being searched could have hundreds or thousands of elements.

Thanks!

like image 653
Travitron Avatar asked Feb 18 '11 00:02

Travitron


People also ask

What is use of Array_unique in PHP?

The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed. Note: The returned array will keep the first array item's key type.

How to unique array PHP?

The array_unique() is a built-in function in PHP and this function removes duplicate values from an array. If there are multiple elements in the array with same values then the first appearing element will be kept and all other occurrences of this element will be removed from the array.

How can I get unique values from two arrays in PHP?

You can use the PHP array_unique() function and PHP array_merge() function together to merge two arrays into one array without duplicate values in PHP.


3 Answers

I think array_unique is slower than in_array but it makes sense if you want to search the array more than one time or if you want to save memory.

Another option is to use array_flip (which will also drop duplicate keys) and then use isset or array_key_exists since they are way faster than in_array, personally I would go this way.

like image 100
Alix Axel Avatar answered Sep 18 '22 10:09

Alix Axel


This comment was worthy to be promoted to an answer:

Just found that array_keys(array_flip($array)); is amazingly faster than array_unique();. About 80% faster on 100 element array, 95% faster on 1000 element array and 99% faster on 10,000+ element array.

Testing here shows that with random (integer) strings which do contain array value repetition, that array_keys(array_flip($array)); method is orders of magnitude faster than other methods.

like image 29
2 revs Avatar answered Sep 22 '22 10:09

2 revs


array_unique is about sqrt(n) times slower then in_array. But if you optimize the data and search again many times, it can be worth

PS: notice that

isset($arr[$key])

works faster then in_array providing the same result

like image 35
Dan Avatar answered Sep 21 '22 10:09

Dan