Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a scenario where array_search is faster than a consecutive array_flip and direct lookup?

Tags:

php

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

like image 766
teeparty Avatar asked May 31 '13 00:05

teeparty


People also ask

Is array_search fast?

The array_search and in_array with $strict = true parameter are the slowest methods in our test.

What is the difference between in_array and array_search?

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.

Is value in array PHP?

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.

Is binary search faster than linear search?

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.

How to check if an array consists of consecutive numbers?

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.

What is the fastest search algorithm?

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.

What does the time-series plot indicate about linear search?

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.


1 Answers

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.

like image 70
Barmar Avatar answered Oct 16 '22 12:10

Barmar