I have a largish array of string that I want to use as a lookup.
I a using in_array(), but I suspect its doing a simple loop through - does anyone know whether the in_array() algo uses a bsearch algo?
I have a largish array of string that I want to use as a lookup.
I a using in_array(), but I suspect its doing a simple loop through - does anyone know whether the in_array() algo uses a bsearch algo?
in_array() uses a linear (O(n)) search rather than a binary (O(log n)) search.
If you want O(log n) or better I would suggest you either put the values you want to search as the keys in an array or you create an index structure that effectively does the same thing.
in_array() is O(n). Also see http://stackoverflow.com/questions/2473989/list-of-big-o-for-php-functions
Since it doesn't require the array to be sorted, I don't see how it could do a binary search.