Jump to content

fastest way to find needle in haystack?


Fog Juice

Recommended Posts

I've been comparing execution time between using in_array($needle, $haystack) and then just using a for loop until I find the correct value. Although in_array is slightly faster, there is no significant performance increase.

 

Does someone know of a more efficient way for finding a needle in a haystack with PHP?

Link to comment
https://forums.phpfreaks.com/topic/171716-fastest-way-to-find-needle-in-haystack/
Share on other sites

Short answer:  No.

 

 

Long answer:  Sort of.  You would have to develop some method of indexing the array.  What I mean is, there would have to be some way that the code would know where to look without doing a full array scan.

 

But, if you did that, you would have to store the indexes or generating them each page load would probably take longer than without them.

Short answer:  No.

 

 

Long answer:  Sort of.  You would have to develop some method of indexing the array.  What I mean is, there would have to be some way that the code would know where to look without doing a full array scan.

 

But, if you did that, you would have to store the indexes or generating them each page load would probably take longer than without them.

 

Is there another server side language that I can mash up with PHP to do the sorting and searching, something that might be faster? I'm just looking for suggestions really, from anyone that might have had to do this in the past.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.