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

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

作者: 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

相关文章

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

    問題 I know that recursion is sometimes a lot cleaner than ...

  • 秋日(個人翻譯

    主啊,是时候了 那曾猖狂的夏日。; 让那影子透过日晷 让那风放肆在这牧场之上; 那枝头未熟的果实 请在给它们多些时...

  • 做一個好翻譯

    親愛的寶貝: 你們都知道,弟弟K喜歡邊走路邊撿石頭玩,你們也都碰過,有時候會有好心的路人,看到弟弟伸手在馬路邊掏來...

  • 人工智能、哲學與筷子

    據說,三十年後會有一百億個機器人跟一百億人共同生活在這星球上。 搞翻譯的朋友最近去聽完一個關於人工智能翻譯...

  • 翻譯

    After graduating from college, Tony decided to start hi...

  • PK:来自星星的傻瓜

    這已經是我第2次看這部電影了,在大陸看的翻譯為《我滴個神呐》,而在台灣則翻譯為《PK,來自星星的傻瓜》。好的、有趣...

  • 快樂快遞

    文:海上明月 我是 小城外卖一小哥 夏日炎炎 我想起骆驼祥子 他是靠双腿,我靠双轮 生活就在奔跑裏面 開花 每天以...

  • 2017-04-29 禪七第一天

    英國詩人蘭德七十五歲生日時所作的小詩《生與死》曾經一度流行 看了好幾個翻譯版本 但最喜歡的還是楊絳先生的翻譯 ...

  • 當日總結20170215

    上午幫一位朋友翻譯財務報表,要求將漢語的翻譯成英語,有些是專業術語,很容易翻譯,有些不是,要很準確的翻譯比較困難,...

  • 成長為一個人

    今天對於我來說,是非比尋常的一天。因為今天我一個人做了好多事。 一個人去上課,一個人去拿快遞,一個人去洗衣房,一個...

网友评论

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

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