📜 ⬆️ ⬇️

PHP: array_search - quick search by array

I have been using the array_search () function for quite a long time to search for values ​​in an array, since I repeatedly heard and read that it works much faster than searching through an array in a loop, but I did not know how much faster it was. Finally got his hands to check and count.

I compared the search speed in an array using this function with the usual array iteration in foreach and while loops. On 10-100 elements of the array, the difference is not noticeable and the time is so short that they can be neglected. But for large arrays, the difference was very significant. With an increase in the array size by an order of magnitude, the search time also increased significantly. With one hundred thousand elements, the foreach speed dropped to 0.013 seconds, and while - to 0.017, while array_search () also slowed down, but still remained an order of magnitude faster - 0.004 seconds. For a large script that works with large arrays, replacing a search in a loop with a search using array_search () is not at all a “flea optimization”.

UPD: added to the break cycles and changed the desired value so that it was in the middle of the array - 5-50-500, etc. The data in the table is updated.
The number of elements in the arrayarray_searchForeach loopWhile loop
ten0.00000680.00000640.0000076
1000.00000780.00001530.0000185
10000.00002090.00011770.0001351
10,0000.00042100.00121280.0018670
100,0000.00396790.01309890.0175215

')
In this regard, I remembered a recent discussion with one of my colleagues at work - whether the programmer needs to know all these built-in functions of the language, or if there is enough “programmer mindset” and general knowledge. Without going into the reasoning about this very stock of mind, I think that all the same it is necessary to know the functions, maybe not the whole syntax in detail, but at least what functions are there and what they can in general terms.

UPD: need a programmer mindset, also needed! And attentiveness with memory will not interfere (inspired by break and range :)

Under habrakat script code, which counted the time:



$ mass = 100,000; // the number of values ​​in the array in which we search
$ search = 50000; // in the array we will search for this value
$ first_result = array (); // array of results to calculate the average of the first variant
$ second_result = array (); // array of results to calculate the average of the second variant
$ third_result = array (); // array of results to calculate the average of the third variant

// create and fill the array
$ test_array = range (0, $ mass-1); // thanks SelenIT))

/ *
$ test_array = array ();
for ($ i = 0; $ i <$ mass; $ i ++)
{
$ test_array [] = $ i;
}
* /

// cycle to calculate averages
for ($ d = 0; $ d <30; $ d ++) {

// *************** Search using array_search *******************

// Start counting time
$ time_start = microtime (1);
// Search
$ key = array_search ($ search, $ test_array, true);
// if found
if ($ key! == FALSE) // exactly! == and not! =, because the number of the first element is 0
{
echo $ test_array [$ key];
}
$ time_end = microtime (1);
// end of time counting

// write to an array of values
$ first_result [] = $ time_end - $ time_start;

// *************** Search through an array with a foreach loop *******************

// Start counting time
$ time_start = microtime (1);
// search itself
foreach ($ test_array as $ ta)
{
if ($ ta == $ search)
{
echo $ ta;
break;
}
}
$ time_end = microtime (1);
// end of time counting

// write to an array of values
$ second_result [] = $ time_end - $ time_start;

// *************** Search through an array with a while loop *******************

// Start counting time
$ time_start = microtime (1);

// determine the length of the array
$ count = count ($ test_array);
$ j = 0;
// search itself
while ($ j <$ count)
{
if ($ test_array [$ j] == $ search) // if found
{
echo $ test_array [$ j];
break;
}
$ j ++;
}
$ time_end = microtime (1);
// end of time counting

// write to an array of values
$ third_result [] = $ time_end - $ time_start;
}

$ srednee1 = array_sum ($ first_result) / count ($ first_result);
$ srednee2 = array_sum ($ second_result) / count ($ second_result);
$ srednee3 = array_sum ($ third_result) / count ($ third_result);

printf ('the first code is executed on average for:% .7f seconds', $ srednee1);
printf ('the second code is executed on average for:% .7f seconds', $ srednee2);
printf ('the third code is executed on average for:% .7f seconds', $ srednee3);

// result:
// first code is executed on average for: 0.0000295 seconds
// the second code is executed on average for: 0.0153386 seconds
// the third code is executed on average for: 0.0226001 seconds

Source: https://habr.com/ru/post/26762/


All Articles