Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.