News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Brian Moon's Blog:
in_array is quite slow
June 06, 2008 @ 09:36:47

Brian Moon had a problem - one of his cron jobs was lasting for much longer (hours!) than it should have been. He tweaked, tested and debugged the script and finally came down to a call to in_array, something he comments on as being "quite slow".

See, this job is importing data from a huge XML file into MySQL. After it is done, we want to compare the data we just added/updated to the data in the table so we can deactivate any data we did not update. [...] We then compared the two arrays by looping one array and using in_array() to check if the value was in the second array. [...] So, that was running for hours with about 400k items. Our data did not contain the value as the key, but it could as the value was unique.

He method, replacing the in_array call that had to do a full array scan for each time through the loop with an isset/unset combo on the unique key, changed the execution time down from about 4 hours to 0.8 seconds.

1 comment voice your opinion now!
inarray compare array unset isset unique key execution time


blog comments powered by Disqus

Similar Posts

Stefan Mischook's Blog: PHP vs. Perl vs. Java – a student's question.

Derick Rethans' Blog: Overloaded properties (__get)

Hasin Hayder's Blog: Unexpected return value from Facebook FQL.query via PHP REST Lib

DevShed: The Isset and Unset Magic Functions in PHP 5

Inside Open Source: Possible addition to array syntax in PHP


Community Events





Don't see your event here?
Let us know!


conference introduction opinion voicesoftheelephpant list language symfony interview unittest install release composer extension configure podcast series api community laravel version

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework