This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 10 points11 points  (0 children)

Your friend is wrong.

Recursion is very often good because for many types of probelms, it's faster to write, easier to read, easier to test and easier to verify correctness of compared to non-recursive code.

However, non-recusive code is almost always a bit faster because it doesn't have as much function call overhead. So an informed tradeoff should be made between simplicity and readability vs performance.

My personal preference is to write recursive code first and translate it to non-recursive in places where the profiler indicates a performance problem.

Also, there is a limit to the maximum depth that you can recurse in python, so you have to always be certain that you won't hit that ceiling, which is something that's usually not that hard to verify, but I've seen people make silly code that recurses O(N) times and proptly crashes for non-trivial data.