What is a practical difference between a loop and recursion

前端 未结 9 2739
无人共我
无人共我 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 23:02

    Stack overflow.

    And no, I don't mean a website or something. I MEAN a "stack overflow".

    0 讨论(0)
  • 2021-02-19 23:03

    In general, a recursive function will consume more stack space (since it's really a large set of function calls), while an iterative solution won't. This also means that an iterative solution, in general, will be faster because.

    I am not sure if this applies to an interpreted language like PHP though, it is possible that the interpreter can handle this better.

    0 讨论(0)
  • 2021-02-19 23:15

    Compared to loops, a function call has its own overhead like allocating stack etc. And in most cases, loops are more understandable than their recursive counterparts.

    Also, you will end up using more memory and can even run out of stack space if the difference between start and stop is high and there are too many instances of this code running simultaneously (which can happen as you get more traffic).

    0 讨论(0)
提交回复
热议问题