Fog Juice Posted August 25, 2009 Share Posted August 25, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/171716-fastest-way-to-find-needle-in-haystack/ Share on other sites More sharing options...
corbin Posted August 25, 2009 Share Posted August 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/171716-fastest-way-to-find-needle-in-haystack/#findComment-905475 Share on other sites More sharing options...
Fog Juice Posted August 25, 2009 Author Share Posted August 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/171716-fastest-way-to-find-needle-in-haystack/#findComment-905486 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.