美文网首页
遞迴和迭代哪個快(待翻譯)

遞迴和迭代哪個快(待翻譯)

作者: franklinyu | 来源:发表于2015-06-01 11:34 被阅读58次

    問題

    I know that recursion is sometimes a lot cleaner than looping, and I'm not asking anything about when I should use recursion over iteration, I know there are lots of questions about that already.

    What I'm asking is, is recursion ever faster than a loop? To me it seems like, you would always be able to refine a loop and get it to perform more quickly than a recursive function because the loop is absent constantly setting up new stack frames.

    I'm specifically looking for whether recursion is faster in applications where recursion is the right way to handle the data, such as in some sorting functions, in binary trees, etc.

    答案

    This depends on the language being used. You wrote 'language-agnostic', so I'll give some examples.

    In Java, C, and Python, recursion is fairly expensive compared to iteration (in general) because it requires the allocation of a new stack frame. In some C compilers, one can use a compiler flag to eliminate this overhead, which transforms certain types of recursion (actually, certain types of tail calls) into jumps instead of function calls.

    In functional programming language implementations, sometimes, iteration can be very expensive and recursion can be very cheap. In many, recursion is transformed into a simple jump, but changing the loop variable (which is mutable) sometimes requires some relatively heavy operations, especially on implementations which support multiple threads of execution. Mutation is expensive in some of these environments because of the interaction between the mutator and the garbage collector, if both might be running at the same time.

    I know that in some Scheme implementations, recursion will generally be faster than looping.

    In short, the answer depends on the code and the implementation. Use whatever style you prefer. If you're using a functional language, recursion might be faster. If you're using an imperative language, iteration is probably faster. In some environments, both methods will result in the same assembly being generated (put that in your pipe and smoke it).

    Addendum: In some environments, the best alternative is neither recursion nor iteration but instead higher order functions. These include "map", "filter", and "reduce" (which is also called "fold"). Not only are these the preferred style, not only are they often cleaner, but in some environments these functions are the first (or only) to get a boost from automatic parallelization — so they can be significantly faster than either iteration or recursion. Data Parallel Haskell is an example of such an environment.

    List comprehensions are another alternative, but these are usually just syntactic sugar for iteration, recursion, or higher order functions.

    個人感想:Python的例子有點特別:我記得Python中數字是immutable的,那按理說Python應該比較喜歡遞迴;但事實上Python社區看起來更支持迭代。

    出處:StackOverflow

    相关文章

      网友评论

          本文标题:遞迴和迭代哪個快(待翻譯)

          本文链接:https://www.haomeiwen.com/subject/upcdqttx.html