Are recursive functions faster than iteration??
Thanks And Regards
Krishna Wadhwani
I m not the right guy to answerâŚbut upto my knowledge both have its own advantageâŚsometimes recursive hlps more instead of iterative or sometimes iterative @vijju @ssjgz helps more.
I would like to share my knowledge on this.
Functions are stored in some memory called stack frame. The size of the stack increases with the increase in recursion depth. Also, when a base case is reached, control is returned to the calling function. The return statement is very costly because it results in control being transferred from one memory location to another, it adds some overhead in execution time.
A simple program that illustrates the execution times of a program doing the same activity iteratively vs recursively.
// Recursion
#include <stdio.h>
int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b%a, a);
}
int main() {
int tests = 10000000;
int a = 10000000;
int b = 1000;
for(int i = 0; i < tests; i++) {
printf("%d\n", gcd(a, b));
a--;
b++;
}
return 0;
}
suman@skynet:~$ time ./gcd1 > out.out
real 0m1.960s
user 0m1.829s
sys 0m0.032s
suman@skynet:~$ time ./gcd1 > out.out
real 0m1.968s
user 0m1.853s
sys 0m0.012s
// Iteration
#include <stdio.h>
int gcd(int a, int b) {
for(int rem; b > 0; rem = a % b, a = b, b = rem);
return a;
}
int main() {
int tests = 10000000;
int a = 10000000;
int b = 1000;
for(int i = 0; i < tests; i++) {
printf("%d\n", gcd(a, b));
a--;
b++;
}
return 0;
}
suman@skynet:~$ time ./gcd2 > out.out
real 0m1.932s
user 0m1.830s
sys 0m0.000s
suman@skynet:~$ time ./gcd2 > out.out
real 0m1.859s
user 0m1.829s
sys 0m0.004s
As you can see, iteration is a bit faster than recursion, although I am unable to show a significant difference between the execution times.
You can try the same with other algorithms like binary search (can be implemented iteratively as well as recursively) to find which is faster.
And as mentioned by @ssjgz , it is not the same with all languages. Recursion is faster in some languages.
Once requirement canât answer everything, better to test your code with both and then submit the one which is faster.
No need to thank me, my answer was obviously incorrect, so donât take it into consideration please. Thatâs why I deleted it in the first place. But others gave some interesting insights and I have to thank to yâall as well.
Yeah , recursive function is faster than iterative onesâŚbut recursive has own advantages
and iterative has ownâŚ
The whole Stack overflow answer:
This depends on the language being used. You wrote âlanguage-agnosticâ, so Iâll give some examples.
In Java, C, and Python, recursion is fairly expensive compared to iteration (in general) because it requires the allocation of a new stack frame. In some C compilers, one can use a compiler flag to eliminate this overhead, which transforms certain types of recursion (actually, certain types of tail calls) into jumps instead of function calls.
In functional programming language implementations, sometimes, iteration can be very expensive and recursion can be very cheap. In many, recursion is transformed into a simple jump, but changing the loop variable (which is mutable) sometimes requires some relatively heavy operations, especially on implementations which support multiple threads of execution. Mutation is expensive in some of these environments because of the interaction between the mutator and the garbage collector, if both might be running at the same time.
I know that in some Scheme implementations, recursion will generally be faster than looping.
In short, the answer depends on the code and the implementation. Use whatever style you prefer. If youâre using a functional language, recursion might be faster. If youâre using an imperative language, iteration is probably faster. In some environments, both methods will result in the same assembly being generated (put that in your pipe and smoke it).
Addendum: In some environments, the best alternative is neither recursion nor iteration but instead higher order functions. These include âmapâ, âfilterâ, and âreduceâ (which is also called âfoldâ). Not only are these the preferred style, not only are they often cleaner, but in some environments these functions are the first (or only) to get a boost from automatic parallelization â so they can be significantly faster than either iteration or recursion. Data Parallel Haskell is an example of such an environment.
List comprehensions are another alternative, but these are usually just syntactic sugar for iteration, recursion, or higher order functions.
Not alwaysâŚ
What Is The Main Difference Between Iterative And Recursive Approach
Hurray Now I Am 2 Coder
Is Java Faster In Recursive Approach Or Iterative Approach
package com.learningjava;
public class Practice_Set_On_Method {
//Recursive Approach
static void table_recursive(int n, int i){
if (i > 10){
return ;
}
System.out.println(n+" * "+i+" = "+n*i);
table_recursive(n, i+1);
}
//Iterative Approach
static void table_iterative(int n){
for (int i = 1; i <= 10; i++){
System.out.println(n*i);
}
}
public static void main(String[] args) {
//Problem 1
//Write A Program To Write Multiplication Tables
//Ans
//Recursive
System.out.println("Recursive");
table_recursive(3, 1);
//Iterative
System.out.println("Iterative");
table_iterative(3);
}
}
Which Will Run Faster??
Which Will Run Faster??