Are recursive functions faster than iteration?

Once requirement can’t answer everything, better to test your code with both and then submit the one which is faster.

Thanks @player_06 @nichke @ssjgz @suman_18733097 @rds_98 To Answer My Question

5 Likes

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.

@suman_18733097 Like Which Languages??

1 Like

@nichke No, Atleast You Spent Time On My Question :slight_smile:

2 Likes

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.

But Why Recursive Are Faster @avx_5801 BTW Thanks For Answer

1 Like

Not always…

Thanks @suman_18733097

2 Likes

What Is The Main Difference Between Iterative And Recursive Approach

1 Like

You can find it on Geeksforgeeks

Hurray Now I Am 2 :star2: Coder

1 Like

Thanks @suman_18733097

1 Like

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??

1 Like

Which Will Run Faster??

1 Like

I think its faster with C++`

Thanks @xenikh_32 For Answering

2 Likes

@gennady.korotkevich which is better approach

2 Likes

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 .

1 Like