Recursion or Iteration?

前端 未结 30 2099
小鲜肉
小鲜肉 2020-11-22 14:44

Is there a performance hit if we use a loop instead of recursion or vice versa in algorithms where both can serve the same purpose? Eg: Check if the given string is a palind

相关标签:
30条回答
  • 2020-11-22 15:07

    As far as I know, Perl does not optimize tail-recursive calls, but you can fake it.

    sub f{
      my($l,$r) = @_;
    
      if( $l >= $r ){
        return $l;
      } else {
    
        # return f( $l+1, $r );
    
        @_ = ( $l+1, $r );
        goto &f;
    
      }
    }
    

    When first called it will allocate space on the stack. Then it will change its arguments, and restart the subroutine, without adding anything more to the stack. It will therefore pretend that it never called its self, changing it into an iterative process.

    Note that there is no "my @_;" or "local @_;", if you did it would no longer work.

    0 讨论(0)
  • 2020-11-22 15:10

    Recursion is better than iteration for problems that can be broken down into multiple, smaller pieces.

    For example, to make a recursive Fibonnaci algorithm, you break down fib(n) into fib(n-1) and fib(n-2) and compute both parts. Iteration only allows you to repeat a single function over and over again.

    However, Fibonacci is actually a broken example and I think iteration is actually more efficient. Notice that fib(n) = fib(n-1) + fib(n-2) and fib(n-1) = fib(n-2) + fib(n-3). fib(n-1) gets calculated twice!

    A better example is a recursive algorithm for a tree. The problem of analyzing the parent node can be broken down into multiple smaller problems of analyzing each child node. Unlike the Fibonacci example, the smaller problems are independent of each other.

    So yeah - recursion is better than iteration for problems that can be broken down into multiple, smaller, independent, similar problems.

    0 讨论(0)
  • 2020-11-22 15:10

    I'm going to answer your question by designing a Haskell data structure by "induction", which is a sort of "dual" to recursion. And then I will show how this duality leads to nice things.

    We introduce a type for a simple tree:

    data Tree a = Branch (Tree a) (Tree a)
                | Leaf a
                deriving (Eq)
    

    We can read this definition as saying "A tree is a Branch (which contains two trees) or is a leaf (which contains a data value)". So the leaf is a sort of minimal case. If a tree isn't a leaf, then it must be a compound tree containing two trees. These are the only cases.

    Let's make a tree:

    example :: Tree Int
    example = Branch (Leaf 1) 
                     (Branch (Leaf 2) 
                             (Leaf 3))
    

    Now, let's suppose we want to add 1 to each value in the tree. We can do this by calling:

    addOne :: Tree Int -> Tree Int
    addOne (Branch a b) = Branch (addOne a) (addOne b)
    addOne (Leaf a)     = Leaf (a + 1)
    

    First, notice that this is in fact a recursive definition. It takes the data constructors Branch and Leaf as cases (and since Leaf is minimal and these are the only possible cases), we are sure that the function will terminate.

    What would it take to write addOne in an iterative style? What will looping into an arbitrary number of branches look like?

    Also, this kind of recursion can often be factored out, in terms of a "functor". We can make Trees into Functors by defining:

    instance Functor Tree where fmap f (Leaf a)     = Leaf (f a)
                                fmap f (Branch a b) = Branch (fmap f a) (fmap f b)
    

    and defining:

    addOne' = fmap (+1)
    

    We can factor out other recursion schemes, such as the catamorphism (or fold) for an algebraic data type. Using a catamorphism, we can write:

    addOne'' = cata go where
               go (Leaf a) = Leaf (a + 1)
               go (Branch a b) = Branch a b
    
    0 讨论(0)
  • 2020-11-22 15:12

    I believe tail recursion in java is not currently optimized. The details are sprinkled throughout this discussion on LtU and the associated links. It may be a feature in the upcoming version 7, but apparently it presents certain difficulties when combined with Stack Inspection since certain frames would be missing. Stack Inspection has been used to implement their fine-grained security model since Java 2.

    http://lambda-the-ultimate.org/node/1333

    0 讨论(0)
  • 2020-11-22 15:12

    In C++ if the recursive function is a templated one, then the compiler has more chance to optimize it, as all the type deduction and function instantiations will occur in compile time. Modern compilers can also inline the function if possible. So if one uses optimization flags like -O3 or -O2 in g++, then recursions may have the chance to be faster than iterations. In iterative codes, the compiler gets less chance to optimize it, as it is already in the more or less optimal state (if written well enough).

    In my case, I was trying to implement matrix exponentiation by squaring using Armadillo matrix objects, in both recursive and iterative way. The algorithm can be found here... https://en.wikipedia.org/wiki/Exponentiation_by_squaring. My functions were templated and I have calculated 1,000,000 12x12 matrices raised to the power 10. I got the following result:

    iterative + optimisation flag -O3 -> 2.79.. sec
    recursive + optimisation flag -O3 -> 1.32.. sec
    
    iterative + No-optimisation flag  -> 2.83.. sec
    recursive + No-optimisation flag  -> 4.15.. sec
    

    These results have been obtained using gcc-4.8 with c++11 flag (-std=c++11) and Armadillo 6.1 with Intel mkl. Intel compiler also shows similar results.

    0 讨论(0)
  • 2020-11-22 15:15

    Comparing recursion to iteration is like comparing a phillips head screwdriver to a flat head screwdriver. For the most part you could remove any phillips head screw with a flat head, but it would just be easier if you used the screwdriver designed for that screw right?

    Some algorithms just lend themselves to recursion because of the way they are designed (Fibonacci sequences, traversing a tree like structure, etc.). Recursion makes the algorithm more succinct and easier to understand (therefore shareable and reusable).

    Also, some recursive algorithms use "Lazy Evaluation" which makes them more efficient than their iterative brothers. This means that they only do the expensive calculations at the time they are needed rather than each time the loop runs.

    That should be enough to get you started. I'll dig up some articles and examples for you too.

    Link 1: Haskel vs PHP (Recursion vs Iteration)

    Here is an example where the programmer had to process a large data set using PHP. He shows how easy it would have been to deal with in Haskel using recursion, but since PHP had no easy way to accomplish the same method, he was forced to use iteration to get the result.

    http://blog.webspecies.co.uk/2011-05-31/lazy-evaluation-with-php.html

    Link 2: Mastering Recursion

    Most of recursion's bad reputation comes from the high costs and inefficiency in imperative languages. The author of this article talks about how to optimize recursive algorithms to make them faster and more efficient. He also goes over how to convert a traditional loop into a recursive function and the benefits of using tail-end recursion. His closing words really summed up some of my key points I think:

    "recursive programming gives the programmer a better way of organizing code in a way that is both maintainable and logically consistent."

    https://developer.ibm.com/articles/l-recurs/

    Link 3: Is recursion ever faster than looping? (Answer)

    Here is a link to an answer for a stackoverflow question that is similar to yours. The author points out that a lot of the benchmarks associated with either recursing or looping are very language specific. Imperative languages are typically faster using a loop and slower with recursion and vice-versa for functional languages. I guess the main point to take from this link is that it is very difficult to answer the question in a language agnostic / situation blind sense.

    Is recursion ever faster than looping?

    0 讨论(0)
提交回复
热议问题