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


i have code adds values array. array later searched in part of code. values added array not unique, it's possible end duplicate values in array being searched. technically speaking, duplicates present in array being searched, code works fine , i'll able find value. want know if value in array being searched, , don't care if it's in array 1 time or 10,000 times.

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

so example, suppose want search array this:

$searchme = array("dog", "cat", "mouse", "dog", "dog", "dog"); 

note "dog" present 4 times. if want search value "dog", in array, work fine , able tell it's present. mentioned above, don't care how many times it's present, want know if it's present @ all.

so should first before searching , search against de-duped array?

$searchme_cleaned = array_unique($searchme); 

i.e., faster searching array duplicates?

please keep in mind although in example array being searched has few elements, real array being searched have hundreds or thousands of elements.

thanks!

i think array_unique slower in_array makes sense if want search array more 1 time or if want save memory.

another option use array_flip (which drop duplicate keys) , use isset or array_key_exists since way faster in_array, go way.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -