r/programming • u/svpino • May 08 '15
Five programming problems every Software Engineer should be able to solve in less than 1 hour
https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour
2.5k
Upvotes
2
u/whooshayay May 09 '15
Doing it recursively adds a significant memory and time overhead. It's far less efficient because you have overhead of time and memory each time you make a function call. The system has to allocate the local variables for that particular instance of the function on the stack, and save the return address. So you're creating one set of additional local variables for each recursive call, and performing additional store and jump instructions.
If your language runtime has tail call recursion, it will convert a specially written recursive implementation into an iterative loop for reasons of performance, but PHP doesn't have this feature.
In your code you can also eliminate $new1 by assigining to the function name instead.