【演算法】轉載:Iterative vs. Recursive Approaches

來源:互聯網
上載者:User

標籤:style   blog   http   io   os   ar   for   strong   sp   

Iterative vs. Recursive ApproachesEyal Lantzman, 5 Nov 2007 CPOL
 
     
   

 

Introduction

This article was originally posted at blogs.microsoft.co.il/blogs/Eyal.

Recursive function – is a function that is partially defined by itself and consists of some simple case with a known answer. Example: Fibonacci number sequence, factorial function, quick sort and more.
Some of the algorithms/functions can be represented in an iterative way and some may not.

Iterative functions – are loop based imperative repetitions of a process (in contrast to recursion which has a more declarative approach).

Comparison between Iterative and Recursive Approaches from Performance ConsiderationsFactorial Collapse | Copy Code
//recursive function calculates n!static int FactorialRecursive(int n){    if (n <= 1) return 1;    return n * FactorialRecursive(n - 1);}//iterative function calculates n!static int FactorialIterative(int n){    int sum = 1;    if (n <= 1) return sum;    while (n > 1)    {        sum *= n;        n--;    }    return sum;}
N Recursive Iterative
10 334 ticks 11 ticks
100 846 ticks 23 ticks
1000 3368 ticks 110 ticks
10000 9990 ticks 975 ticks
100000 stack overflow 9767 ticks

As we can clearly see, the recursive is a lot slower than the iterative (considerably) and limiting (stackoverflow).

The reason for the poor performance is heavy push-pop of the registers in the ill level of each recursive call.

Fibonacci Collapse | Copy Code
//--------------- iterative version ---------------------    static int FibonacciIterative(int n){    if (n == 0) return 0;    if (n == 1) return 1;            int prevPrev = 0;    int prev = 1;    int result = 0;            for (int i = 2; i <= n; i++)    {        result = prev + prevPrev;        prevPrev = prev;        prev = result;    }    return result;}    //--------------- naive recursive version --------------------- static int FibonacciRecursive(int n){    if (n == 0) return 0;    if (n == 1) return 1;            return FibonacciRecursive(n - 1) + FibonacciRecursive(n - 2);}    //--------------- optimized recursive version ---------------------static Dictionary<int> resultHistory = new Dictionary<int>();static int FibonacciRecursiveOpt(int n){    if (n == 0) return 0;    if (n == 1) return 1;    if (resultHistory.ContainsKey(n))         return resultHistory[n];    int result = FibonacciRecursiveOpt(n - 1) + FibonacciRecursiveOpt(n - 2);    resultHistory[n] = result;            return result;}
N Recursive Recursive opt. Iterative
5 5 ticks 22 ticks 9 ticks
10 36 ticks 49 ticks 10 ticks
20 2315 ticks 61 ticks 10 ticks
30 180254 ticks 65 ticks 10 ticks
100 too long/stack overflow 158 ticks 11 ticks
1000 too long/stack overflow 1470 ticks 27 ticks
10000 too long/stack overflow 13873 ticks 190 ticks
100000 too long/stack overflow too long/stack overflow 3952 ticks

As before, the recursive approach is worse than iterative however, we could apply memorization pattern (saving previous results in dictionary for quick key based access), although this pattern isn‘t a match for the iterative approach (but definitely an improvement over the simple recursion).

Notes
  1. The calculations may be wrong in big numbers, however the algorithms should be correct.
  2. For timer calculations, I used System.Diagnostics.Stopwatch.
Points of Interest
  1. Try not to use recursion in system critical locations.
  2. Elegant solutions not always the best performing when used in "recursive situations".
  3. If you required to use recursion, at least try to optimize it with dynamic programming approaches (such as memorization).

轉自:http://www.codeproject.com/Articles/21194/Iterative-vs-Recursive-Approaches

 關於Tail Recursive

It is possible that recursion will be more expensive, depending on if the recursive function is tail recursive (last line is recursive call). Tail recursion should be recognized by the compiler and optimized to its iterative counterpart (while maintaining the concise, clear implementation you have in your code).

I would write the algorithm in the way that makes the most sense and is the most clear for the poor sucker (be it yourself or someone else) that has to maintain the code in a few months or years. If you run into performance issues, then profile your code, and then and only then look into optimizing by moving over to an iterative implementation. You may want to look into memoization and dynamic programming.

轉自: http://stackoverflow.com/questions/72209/recursion-or-iteration

參考資料:

尾調用:

http://zh.wikipedia.org/wiki/%E5%B0%BE%E8%B0%83%E7%94%A8

http://en.wikipedia.org/wiki/Tail_call

【演算法】轉載:Iterative vs. Recursive Approaches

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.