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??
Recursion is when a statement in a function calls itself repeatedly . The iteration is when a loop repeatedly executes until the controlling condition becomes false .
