DCOM-59: ArrayCollection contains function - switch from in_array to foreach #659

doctrinebot opened this Issue Aug 10, 2011 · 2 comments

2 participants


Jira issue originally created by user pem:

The function contains uses inarray to determine if an element provided as parameter is found in the array $this->elements, and performs a strict comparison.
There might be a way to improve the performances of this function by using a foreach + if

In attachement you can find a small bench file to show the differences between in_array and foreach on object search.
You might want to run the bench with a doctrine Object to verify if this changes anything to the result.
For simple Objects, foreach is much faster when the array has less than 30k elements. Over 30k the difference flattens, and might revert around 50k (meaning that in_array might start being faster when the array has more than 50k entries).

Maybe we could provide two functions, one for smaller than 40-50k items (using foreach), and one for bigger arrays (using in_array) ?


Comment created by @guilhermeblanco:

I created more test cases and indeed foreach is around 40% faster than in_array. I applied the change in our suite, this should be available in new versions of Doctrine Common.


Issue was closed with resolution "Fixed"

@doctrinebot doctrinebot added this to the 2.2 milestone Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment