What is a practical difference between a loop and recursion

前端 未结 9 2685
无人共我
无人共我 2021-02-19 22:42

I am currently working in PHP, so this example will be in PHP, but the question applies to multiple languages.

I am working on this project with a fiend of mine, and as

9条回答
  •  天涯浪人
    2021-02-19 22:50

    A loop will be faster because there's always overhead in executing an extra function call.

    A problem with learning about recursion is a lot of the examples given (say, factorials) are bad examples of using recursion.

    Where possible, stick with a loop unless you need to do something different. A good example of using recursion is looping over each node in a Tree with multiple levels of child nodes.

提交回复
热议问题