Why should recursion be preferred over iteration?

前端 未结 18 1193
既然无缘
既然无缘 2020-11-29 18:50

Iteration is more performant than recursion, right? Then why do some people opine that recursion is better (more elegant, in their words) than iteration? I really don\'t see

18条回答
  •  一整个雨季
    2020-11-29 19:45

    "Iteration is more performant than recursion" is really language- and/or compiler-specific. The case that comes to mind is when the compiler does loop-unrolling. If you've implemented a recursive solution in this case, it's going to be quite a bit slower.

    This is where it pays to be a scientist (testing hypotheses) and to know your tools...

提交回复
热议问题