Announcement

  •  » Off topic
  •  » [Coding tip] big array and value checking

#1 2014-04-24 14:11:24

plg
Piwigo Team
Nantes, France, Europe
2002-04-05
13180

[Coding tip] big array and value checking

Hi,

I have an array with 30k items (IP addresses, to ban on piwigo.org wiki). Until now I had this code:

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:

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...)


Latest blog post (November 9th 2018) Why Flickr could not remain free for ever

Offline

 

#2 2014-04-24 14:41:30

mistic100
Former Piwigo Team
Lyon (FR)
2008-09-27
3271

Re: [Coding tip] big array and value checking

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

 

#3 2014-05-12 12:40:14

billy88
Member
2014-05-12
11

Re: [Coding tip] big array and value checking

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

 
  •  » Off topic
  •  » [Coding tip] big array and value checking

Board footer

Powered by FluxBB

github twitter facebook google+ newsletter Donate Piwigo.org © 2002-2019 · Contact