Imagine you were to search an array of N elements and perform Y Searches on the array values to find the corresponding keys; you can either do Y array_search
's or do one array_flip
and Y direct lookups. Why is the first method alot slower than the second method? Is there a scenario where the first method becomes faster than the second one?
You can assume that keys and values are unique
The array_search and in_array with $strict = true parameter are the slowest methods in our test.
The main difference between both the functions is that array_search() usually returns either key or index whereas in_array() returns TRUE or FALSE according to match found in search. Value: It specifies the value that needs to be searched in an array.
The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.
If we use the Linear search, it can be faster than Binary search (in rare cases, it might happen) but it also can be slower (mostly slower than binary search). It reinforces that the runtime stability in the Linear search algorithm is totally poor.
Given an unsorted array of numbers, write a function that returns true if the array consists of consecutive numbers. a) If the array is {5, 2, 3, 1, 4}, then the function should return true because the array has consecutive numbers from 1 to 5.
Illustration of the linear search algorithm. The runtime depends on the number of elements (Image by Author) According to a simulation conducted by researchers, it is known that Binary search is commonly the fastest searching algorithm. A binary search is performed for the ordered list.
The time-series plot indicates that there is a positive correlation between the number of elements in the list and runtime for the Linear search algorithm. This is why the method called linear because the runtime increases when the number of elements increases. It will be a problem for app performances.
Array keys are hashed, so looking them up just requires calling the hash function and indexing into the hash table. So array_flip()
is O(N) and looking up an array key is O(1), so Y
searches are O(Y)+O(N).
Array values are not hashed, so searching them requires a linear search. This is O(N), so Y searches are O(N*Y).
Assuming values being searched for are evenly distributed through the array, the average case of linear search has to compare N/2 elements. So array_flip()
should take about the time of 2 array_search()
calls, since it has to examine N elements.
There's some extra overhead in creating the hash table. However, PHP uses copy-on-write, so it doesn't have to copy the keys or values during array_flip()
, so it's not too bad. For a small number of lookups, the first method may be faster. You'd have to benchmark it to find the break-even point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With