Why is recursion in python so slow?

前端 未结 2 986
攒了一身酷
攒了一身酷 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.

提交回复
热议问题