2

Recursion or iteration, and why?

Comments
  • 1
    Depends on usecase but iteration is a lot faster
  • 1
    Recursion is cooler. Plus it makes for more understandable course (occasionally)
  • 2
    Recursion because it makes me feel like I learned something from my CS courses
  • 0
    iteration cause it's usually easier to read
  • 1
    Without thinking at the purpose, i prefer recursion.

    So cool
  • 0
    Really depends on the situation.
    Recursion elegantly solves problems that would otherwise require a lot of condition checking for an iterative solution.
  • 0
    Unfortunately python limits recursion 😞
  • 0
    Personally, I do a lot of work with embedded systems and RTOS, so I already don't have a big stack to work with most of the time.
    I'd much rather have a bigger, iteration-based codebase that is harder to read, because that all exists on SRAM during runtime, and doesn't take up a lot of space.
    Invocation records exist on the user stack, which fills up very fast unless you fork every recursive task into a child process. Then you're at least giving the recursion its own context that it can fill up, and the parent is somewhat protected if the child runs away. Even then you have to worry about communicating the results of your recursive call back to the parent, which adds a slight performance hit, and you have to make sure you're getting the right info from the right process, and you run the risk of zombied processes building up.
    I guess I prefer to use iteration because of space and time concerns, and I'd much rather use comments in a complicated but fast loop.
  • 0
    I wouldn't rely on recursion for complicated tasks unless I shunted the recursion inside a child process.
    Small recursive tasks I'll either try to make iterative, or if it's really dead-simple, just do the recursion right then and there. But it would need to be a small task.
  • 0
    Plus, putting the recursive call inside a child process means the parent can complete other code while it's waiting for the recursion to complete.
    You could even have multiple, concurrent children performing seperate recursive tasks under the same parent, and complete other code while waiting for the recursive tasks to finish up.
  • 1
    Tail recursions, the best of two worlds :)
  • 0
    @DemonFtIllusion Touché sir. Have a ++ and welcome to devRant!
Add Comment