Hi,
I have an array with 30k items (IP addresses, to ban on piwigo.org wiki). Until now I had this code:
$ip_addresses = ('1.2.3.4', '1.2.3.5'); // imagine 30k items foreach ($test_ips as $test_ip) // many, ie ~25k { if (in_array($test_ip, $ip_addresses)) { // ip found } }
It was taking ~19s.
New code:
$ip_addresses = ('1.2.3.4' => 1, '1.2.3.5' => 1); // 30k values become keys foreach ($test_ips as $test_ip) // many, ie ~25k { if (isset($ip_addresses[$test_ip])) { // ip found } }
New time = 40ms.
The bigger is your $ip_addresses array and the bigger is your $test_ips array, the slower the first code was (of course...)
Offline
for the math in_array is O(n) and isset is O(1) (almost)
in many cases you'll need an array_flip, don't know the complexity (certainly O(n) ) but it quickly becomes way faster anyway if you have many checks to perform
and don't forget that isset returns false is the value is null, use array_key_exists then
--
interesting post about array functions complexities
http://stackoverflow.com/a/2484455/1207670
Offline
Hi, in this particular case I would try one of often forgettable build-in functions for arrays. I'm talking about array_intersect function family (array_intersect or array_intersect_key in that case).
Using them you can avoid the loop altogether. I haven't test it with big arrays, but with smaller ones I was astonished by speed.
Let me know how it will work if you're going to try it.
Offline