Skip to main content
added 182 characters in body
Source Link

At the machine code level a loop is just a conditional jump and a test whereas a naively implemented recursion involves jumps, stack frame pushes, stack frame pops, and returns.

Howbeit, most recursive functions can be rewritten to avoid stack frame push/pop by exerting tail recursion which yield an equivalent to an iterative loop at the machine code level.

The use of stack frames however might be benefiting in certain scenarios as it allows stack tracebacks, a la debugging; In contrast, a loop employs destructive state update and therefore limits tracebacks.

Furthermore, from a mathematical perspective the recursive approach is preferred as the solutions are well matched to the abstract mathematical entity (i.e. tree data structure).

I am assuming your question is in relation to a C-like language as the answers will vary depending on the language, compiler and the dataset; For a more comprehensive overview refer these slides.

At the machine code level a loop is just a conditional jump and a test whereas a naively implemented recursion involves jumps, stack frame pushes, stack frame pops, and returns.

Howbeit, most recursive functions can be rewritten to avoid stack frame push/pop by exerting tail recursion which yield an equivalent to an iterative loop at the machine code level.

The use of stack frames however might be benefiting in certain scenarios as it allows stack tracebacks, a la debugging; In contrast, a loop employs destructive state update and therefore limits tracebacks.

I am assuming your question is in relation to a C-like language as the answers will vary depending on the language, compiler and the dataset; For a more comprehensive overview refer these slides.

At the machine code level a loop is just a conditional jump and a test whereas a naively implemented recursion involves jumps, stack frame pushes, stack frame pops, and returns.

Howbeit, most recursive functions can be rewritten to avoid stack frame push/pop by exerting tail recursion which yield an equivalent to an iterative loop at the machine code level.

The use of stack frames however might be benefiting in certain scenarios as it allows stack tracebacks, a la debugging; In contrast, a loop employs destructive state update and therefore limits tracebacks.

Furthermore, from a mathematical perspective the recursive approach is preferred as the solutions are well matched to the abstract mathematical entity (i.e. tree data structure).

I am assuming your question is in relation to a C-like language as the answers will vary depending on the language, compiler and the dataset; For a more comprehensive overview refer these slides.

Source Link

At the machine code level a loop is just a conditional jump and a test whereas a naively implemented recursion involves jumps, stack frame pushes, stack frame pops, and returns.

Howbeit, most recursive functions can be rewritten to avoid stack frame push/pop by exerting tail recursion which yield an equivalent to an iterative loop at the machine code level.

The use of stack frames however might be benefiting in certain scenarios as it allows stack tracebacks, a la debugging; In contrast, a loop employs destructive state update and therefore limits tracebacks.

I am assuming your question is in relation to a C-like language as the answers will vary depending on the language, compiler and the dataset; For a more comprehensive overview refer these slides.