Why should recursion be preferred over iteration?

前端 未结 18 1152
既然无缘
既然无缘 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:39

    I don't think there's anything intrinsically less performant about recursion - at least in the abstract. Recursion is a special form of iteration. If a language is designed to support recursion well, it's possible it could perform just as well as iteration.

    In general, recursion makes one be explicit about the state you're bringing forward in the next iteration (it's the parameters). This can make it easier for language processors to parallelize execution. At least that's a direction that language designers are trying to exploit.

    0 讨论(0)
  • 2020-11-29 19:41

    In Java, recursive solutions generally outperform non-recursive ones. In C it tends to be the other way around. I think this holds in general for adaptively compiled languages vs. ahead-of-time compiled languages.

    Edit: By "generally" I mean something like a 60/40 split. It is very dependent on how efficiently the language handles method calls. I think JIT compilation favors recursion because it can choose how to handle inlining and use runtime data in optimization. It's very dependent on the algorithm and compiler in question though. Java in particular continues to get smarter about handling recursion.

    Quantitative study results with Java (PDF link). Note that these are mostly sorting algorithms, and are using an older Java Virtual Machine (1.5.x if I read right). They sometimes get a 2:1 or 4:1 performance improvement by using the recursive implementation, and rarely is recursion significantly slower. In my personal experience, the difference isn't often that pronounced, but a 50% improvement is common when I use recursion sensibly.

    0 讨论(0)
  • 2020-11-29 19:41

    on ntfs UNC max path as is 32K
    C:\A\B\X\C.... more than 16K folders can be created...

    But you can not even count the number of folders with any recursive method, sooner or later all will give stack overflow.

    Only a Good lightweight iterative code should be used to scan folders professionally.

    Believe or not, most top antivirus cannot scan maximum depth of UNC folders.

    0 讨论(0)
  • 2020-11-29 19:43

    Several things:

    1. Iteration is not necessarily faster
    2. Root of all evil: encouraging something just because it might be moderately faster is premature; there are other considerations.
    3. Recursion often much more succinctly and clearly communicates your intent
    4. By eschewing mutable state generally, functional programming languages are easier to reason about and debug, and recursion is an example of this.
    5. Recursion takes more memory than iteration.
    0 讨论(0)
  • 2020-11-29 19:44

    Try implementing depth-first search recursively and iteratively and tell me which one gave you an easier time of it. Or merge sort. For a lot of problems it comes down to explicitly maintaining your own stack vs. leaving your data on the function stack.

    I can't speak to Haskell as I've never used it, but this is to address the more general part of the question posed in your title.

    0 讨论(0)
  • 2020-11-29 19:44

    Recursion is the typical implementation of iteration. It's just a lower level of abstraction (at least in Python):

    class iterator(object):
        def __init__(self, max):
            self.count = 0
            self.max = max
    
        def __iter__(self):
            return self
    
        # I believe this changes to __next__ in Python 3000
        def next(self):
            if self.count == self.max:
                raise StopIteration
            else:
                self.count += 1
                return self.count - 1
    
    # At this level, iteration is the name of the game, but
    # in the implementation, recursion is clearly what's happening.
    for i in iterator(50):
        print(i)
    
    0 讨论(0)
提交回复
热议问题