Re: Re: Fuzzy Array Search
| From: | Shawn McKenzie | Date: | Tue, 07 Jun 2011 21:25:12 +0000 |
| Subject: | Re: Re: Fuzzy Array Search | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-313450@lists.php.net to get a copy of this message | ||
On 06/07/2011 03:57 PM, Floyd Resler wrote:
>
> On Jun 7, 2011, at 4:42 PM, Alex Nikitin wrote:
>
>> If you don't need the location, you can implode the array and use preg
>> match, quickly testing it, that gives you about 4.5 times performance
>> increase, but it wont give you the location, only if a certain value exists
>> within the array... You can kind of do some really clever math to get your
>> search parameters from there, which would be feasible on really large data
>> sets, but if you want location, you will have to iterate at some point...
>>
>> (sorry i keep on hitting reply instead of reply to all)
>>
>> --
>> The trouble with programmers is that you can never tell what a programmer is
>> doing until it’s too late. ~Seymour Cray
>>
>>
>>
>> On Tue, Jun 7, 2011 at 2:57 PM, Shawn McKenzie <nospam@mckenzies.net> wrote:
>>
>>> On 06/07/2011 12:45 PM, Floyd Resler wrote:
>>>> What would be the easiest way to do a fuzzy array search? Can I do this
>>> without having to step through the array?
>>>>
>>>> Thanks!
>>>> Floyd
>>>>
>>>
>>> I use preg_grep()
>>>
>>> --
>>> Thanks!
>>> -Shawn
>>> http://www.spidean.com
>>>
>
> I actually do need the location since I need to get the resulting match. I went ahead and
> tried to iterate the array and it was MUCH faster than I expected it to be! Of course, considering
> the machine I'm running this on is a monster (2.66 GHz 8 cores, 24GB of RAM) it shouldn't
> have surprised me!
>
> Thanks!
> Floyd
>
If you are using a straight equality comparison then the loop would be
faster (but then array search would probably be better), however if you
need to use a preg_match() in the loop ("fuzzy search"), then
preg_grep() will be much faster than the loop.
LOOP WITH PREG_MATCH: 100000
0.435957 seconds
PREG_GREP: 100000
0.085604 seconds
LOOP WITH IF ==: 100000
0.044594 seconds
PREG_GREP: 100000
0.091519 seconds
--
Thanks!
-Shawn
http://www.spidean.com