7

This is something I've been wondering for a while. Is performance always better when using native PHP functions than their PHP loop equivalents? Why or why not?

Here are two examples to illustrate the question:

  • Say I have a large array with 1000 elements. Each value is just an integer userid. I want to see if a particular userid is in the array, and I happen to know it will be towards the end of the array (as it would have been added recently). I have two options: a) do a regular PHP foreach loop that goes through the whole array until it finds the userid, or b) do an array_reverse() on the array and do the same foreach loop until it finds the id (if it exists, it will end this loop sooner). Which is faster?

My gut tells me the first option is faster since it does one loop, and the second option is slower because behind the scenes, array_reverse() is also doing some kind of loop (in C), thereby requiring two loops for the operation.

  • If I have a large multidimensional array where each value is [messageid, message], will it be slower to use a foreach loop to find a particular message by id, than it is to set the messageid as the key for the element and doing isset(array[messageid]) && array[messageid]?

Edit: just to clarify, I'm aware the first example can use array_search(), since this is a very simplified example. The main question isn't which one is faster (as benchmarks can let me know easily), but why, as in, what's going on behind the scenes?

15
  • 4
    "I want to see if a particular userid is in the array" - use array_search() or in_array(), which will be faster than scanning the loop with foreach() in either direction. However, I should think that reversing and finding it quickly might be faster than finding it slowly under certain array sizes. Commented Sep 4, 2014 at 18:31
  • 3
    Don't ask questions like these, instead perform your own benchmarks and profiling, then publish your results. Commented Sep 4, 2014 at 18:32
  • This cannot be generally answered, since everything is "native PHP" in some way. In your first example, array_reverse is a somewhat large operation, but doing it may be faster than a foreach loop with some additional comparison. Of course, for ($i = count($arr) - 1; $i >= 0; $i--) will likely be even faster. In your second case accessing an element by index will be orders of magnitude faster than looping, they're entirely different classes of operation. Commented Sep 4, 2014 at 18:33
  • 2
    @Dai he is not asking whether it is faster, he is asking why it is. Benchmarking won't tell you that. Commented Sep 4, 2014 at 18:36
  • 3
    the larger your data set, the less important the overhead of the programming language becomes and the more important the efficiency of your algorithm is. so for smaller datasets an algorithm with more operations (i.e. 2 loops in your example) maybe faster using native php but for larger datasets it will usually come down to the algorithm and not whether it's using native functions Commented Sep 4, 2014 at 18:46

2 Answers 2

1

You can see PHP benchmarks of time consumed by functions here, in my opinion they are not terribly different. Why? More code or less code, less code or more basic comparison functions take less floating point operations than an actual function, then again those functions operate in basic comparison methods like array_reverse() which are not terribly time-consuming and processing required.

http://www.phpbench.com/

EDIT: I agree with FuzzyTree in the fact that you should focus on the efficiency of the algorithm and not the functions themselves.

Sign up to request clarification or add additional context in comments.

Comments

-2

array_xxx() functions are composed from loops and they are the most optimized as they can be. I don't think you can make any better on your own with your "handmade" foreach/for/while loops. So I prefer using predeclared functions.
(By the way, in the most primitive form of programming language there is only one kind of loop but we are able to modulate it into three forms :) )

2 Comments

There's some detail missing here. The array_search is faster as it is calling a native machine-code function, whereas the equivalent PHP has parsing and interpretation steps that will slow it down. That said, the array functions are probably optimised as they can be in PHP, but if a programmer needed more speed, they'd write the program in C themselves, and the result could well be faster. This is because a native C program probably does not need the complexity of PHP's internal array system.
@halfer I thought we are talking in the borders of PHP so why are you messing here a native C? Another one can say that you should use Assembler and make it even faster man.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.