Why is recursion in python so slow?

前端 未结 2 987
攒了一身酷
攒了一身酷 2020-12-06 06:47

So I was messing around in idle with recursion, and I noticed that a loop using recursion was much slower then a regular while loop, and I was wondering if anyone knew why.

相关标签:
2条回答
  • 2020-12-06 07:02

    Recursion is fairly expensive compared to iteration (in general) because it requires the allocation of a new stack frame.

    0 讨论(0)
  • 2020-12-06 07:21

    You've written your function to be tail recursive. In many imperative and functional languages, this would trigger tail recursion elimination, where the compiler replaces the CALL/RETURN sequence of instructions with a simple JUMP, making the process more or less the same thing as iteration, as opposed to the normal stack frame allocation overhead of recursive function calls. Python, however, does not use tail recursion elimination, as explained in some of these links:

    http://neopythonic.blogspot.com/2009/04/tail-recursion-elimination.html

    http://metapython.blogspot.com/2010/11/tail-recursion-elimination-in-python.html

    As you can see from the links, there are reasons it doesn't exist by default, and you can hack it in in a number of ways, but by default, Python prizes using things like generator functions to create complicated sequences of instructions, versus recursion.

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