By user137717


2014-08-13 20:59:26 8 Comments

I've read posts where people say certain compilers will implement recursion as loops but the hardware implements loops as recursion and vice versa. If I have a recursive function and an iterative function in my program, can someone please explain how the compiler and hardware are going to interpret each one? Please also address any performance benefits of one over the other if the choice of implementation does not clearly favor one method like using recursion for mergesort.

1 comments

@kraskevich 2014-08-13 22:00:47

Ok, here is a brief answer:

1)A compiler can optimize tail recursive calls. But it is usually not a loop, but rather a stack frame reuse. However, I have never heard of any compiler that converts a loop into recursion(and I do not see any point of doing so: it would use additional stack space, likely to work slower and can lead to the change of semantics(stackoverflow instead of an infinite loop)).

2)I would say that it is not correct to speak about hardware implementing loops, because hardware itself does not implement loops. It has instructions(like conditional jumps, arithmetical operations and so on) which are used to implement loops.

@user137717 2014-08-14 00:35:35

thanks, i appreciate it

Related Questions

Sponsored Content

19 Answered Questions

[SOLVED] Accessing the index in 'for' loops?

  • 2009-02-06 22:47:54
  • Joan Venge
  • 1871774 View
  • 3226 Score
  • 19 Answer
  • Tags:   python loops list

35 Answered Questions

[SOLVED] How do I break out of nested loops in Java?

  • 2009-05-20 09:07:43
  • boutta
  • 1068776 View
  • 1725 Score
  • 35 Answer
  • Tags:   java loops

12 Answered Questions

[SOLVED] Looping through the content of a file in Bash

  • 2009-10-05 17:52:54
  • Peter Mortensen
  • 1390238 View
  • 1200 Score
  • 12 Answer
  • Tags:   linux bash loops unix io

40 Answered Questions

[SOLVED] Loop through an array in JavaScript

37 Answered Questions

[SOLVED] How do I loop through or enumerate a JavaScript object?

27 Answered Questions

[SOLVED] What is tail recursion?

43 Answered Questions

[SOLVED] JavaScript closure inside loops – simple practical example

28 Answered Questions

[SOLVED] Recursion or Iteration?

11 Answered Questions

[SOLVED] Ways to iterate over a list in Java

12 Answered Questions

[SOLVED] Is recursion ever faster than looping?

Sponsored Content