answersLogoWhite

0

Generally no. Every time a function is called, the current CPU register state must be saved, so the current function's local arguments and the return address, as well as the arguments to the called function, need to be pushed onto the call stack before a jump is made to that function. When entered, its arguments are popped off the stack in reverse order and the interesting part of the function is executed. when the function returns, the return address is popped and execution jumps to that address. For small functions it can actually take more time to call and return from the function than it does to execute the function itself. AMD claims any function with less than 50 instructions should be inline expanded. Unfortunately recursive functions where the depth of recursion is variable cannot be inline expanded, or can only be partially expanded up to a predetermined maximum depth.

Most texts on optimization suggest trading iteration for recursion. Highly optimized versions of most popular recursive sorts (merge, quick), have had their recursions replaced with local stacks that are more efficient than the call stack.

If you have found code that executes faster using recursion, keep in mind that compilers are very evolved at this point and may very well have recognized the recursion and replaced it with an iterative sequence, particularly when the recursion is a tail recursion (where the recursion becomes redundant because there is no need to save the state of the current instance before returning to the previous instance), or some 'built in' that may be superior to your version. It's even possible your processor recognizes the pattern and is able to accelerate the repeated calls.

User Avatar

Wiki User

10y ago

What else can I help you with?

Related Questions

Which is faster to find factorial whether recursive or dowhile?

recursion is always slower than iteration


Which one is advantageous recrsion or iteration?

Depends... I teach algorithms and advice my students to choose whichever they find simpler to implement. Sometimes recursion is more intuitive than iteration and viceversa. All that is recursive can be done iterative and the other way around. The only problem you would have with recursion is having a stack overflow (if you call the recursive method too many times).


What is iteration in c programming?

Iteration means what it says: the act of repeating a process. Each repetition of the process is itself an iteration, and the results of one iteration can be applied in the next iteration. Counting from 1 to 10 is an example of an iterative process because each iteration increments the counter by 1. Iteration should not be confused with recursion. Although similar, a recursion occurs when a function calls itself. Such functions are known as recursive functions and these make use of the call stack to remember the "state" of each function prior to the next call, thus allowing those states to be restored when the recursive calls return (known as "unwinding"). Since the call stack must be maintained regardless of the depth of calls, recursive routines that do not need to remember the state of each recursion are inefficient, and are often better implemented as iterative loops. However, this may require nested iterations and, if the depth is too variable or complex to be calculated at compile time, or the maximum depth would be greater than 16 then the cost of recursion will often be preferred to the increased code size iteration would incur. Even so, recursive functions can often be inline expanded by the compiler as iterative functions, thus simplifying the source code without sacrificing the performance.


Will recursion runs faster than loops?

no.


How do you overcome limitations of stacks in polygon filling?

You overcome limitations of the stack in polygon filling, or in any other algorithm, far that matter, but using an iterative technique, rather than a recursive technique. Recursion is quite useful, and can simplify algorithm design. Polygon filling, however, is a class of algorithm can potentially have a very deep recursion depth. This causes stress on the stack, hence the need for iteration.


What is an indefinite iteration?

Indefinite iteration refers to a looping construct in programming that continues to execute a block of code until a specific condition is met, rather than a predetermined number of times. Common examples include while and do-while loops, where the iteration depends on a condition that may change during execution. This allows for greater flexibility, as the number of iterations is not fixed and can adapt based on user input or other factors. Indefinite iteration is useful for scenarios where the exact number of repetitions is unknown in advance.


What is unique about a DOWHILE loop?

A DO-WHILE loop will always execute at least one iteration of the loop body. This is because the condition that controls the loop comes at the end of the loop, rather than at the beginning as per a WHILE or FOR loop.


What is the difference between looping and recursion?

Both Iteration and Recursion run a part of an algorithm repeatedly. We typically use them when, in a part of an algorithm, we have to do or calculate something (i,e. the logic remains the same though the data changes) for a certain number of times. However,in Recursion, we simply run a loop for a certain number of times in the algorithm, such as printing a statement ten times is recursion. In Iteration, we specifically run a loop, such that, the second run of the loop makes use of the computation/result from the first run, the third run (or iteration) makes use of the result from the second run and so on. Hence, in Iteration, the n th run of the loop makes use of the result from the n-1 th run. Consider the following example for calculating the summation, which we will denote as sum(n), meaning n + n-1 + n-2 + n-3 + ... + 2 + 1 + 0. Hence, sum(5) = 5+4+3+2+1+0 = 15. Now we will write psuedo-code to calculate the sum(n) using Iteration and Recursion. Iterative Version:- ----------------- start sum(n) { i = 0; total = 0; do while i is not greater than n, { total = total + i; i = i + 1; } return total; } Recursive Version:- ----------------- start sum(n) { if n is equal to 0, then return(0); otherwise, return (n + sum(n - 1)); } Notice that the Iterative version calculates the sum by repeatedly accumulating the sum in the variable total. In contrast, the recursive version simply repeatedly calls itself, each time with a decremented value. .


How many types of recursion are there in c language?

Recursion in c language is a method where the function calls itself, within or outside the scope. Using Recursion, complicated problems can be divided into smaller parts so that solving them becomes more manageable. The recursion technique is available in Java, JavaScript, and C++.serves the same purpose. The type of Recursion in C • Direct Recursion • Indirect Recursion. Direct Recursion Recursion can call the function n-number of times. In the case of direct Recursion, the function calls itself inside the same position or in the local scope Direct Recursion problems are the Fibonacci series, a program to print 50 natural numbers. Indirect Recursion In the case of Indirect Recursion, a function X calls function Y, and function Y calls any function Z. Under certain conditions, function Z calls function A. In this case, function A is indirectly related to function Z. Indirect Recursion is also known as mutual Recursion, as more than one function runs a program. It is a two-step recursive function call process for making a recursive function call. Below mentioned are also type of Recursion: Tail Recursion No Tail/Head Recursion Linear Recursion Tree Recursion Tail Recursion A function is said to be tail recursion if it calls itself and also calls the last or the previous statement executed in the process. Head Recursion A function is said to be Head Recursion if it calls itself and also calls the first or the beginning statement executed in the process. Linear Recursion A function is said to be a linear recursive function if it makes a single call to itself each time the procedure executes itself and grows linearly depending on the size of the problem. Tree Recursion Tree Recursion is different from linear Recursion. Rather than making only one call to itself, that function makes more than one recursive call to the process within the recursive function. Following are the steps to solve the recursive problem in C: Step 1: Create a function and assign the work a part should do. Step 2: Select the subproblem and assume that the function already works on the problem. Step 3: Get the answer to the subproblem and use it to resolve the main issue. Step 4: The 90% of the problem defined is solved.


Shaiju is shorter than anil but she is taller than anup renjith is shorter than anil and shorter than anup raj is taller than renjith but shorter than anil if the statements above are true?

Shaiju is shorter than Anil, but she is taller than Anup. Renjith is shorter than Anil, and shorter than Anup. Raj is taller than Renjith, but shorter than Anil. If the statements above are true, one can validly conclude that Sathish is shorter than Shaiju if it is true that: Shaiju is equal in height to Raj Raj is equal in height to Sathish Sathish is taller than Renjith but shorter than Raj Sathish is shorter than Anil but taller than Anup Raj is taller than Sathish, but shorter than Anup


What is shorter than you and found in North America?

"I" is shorter than "you"


What are the differences between iteration and recursion?

In computer programming, both iteration and recursion define a type of loop. With iteration, the loop makes use of the current instance of the function in which it appears. When we start a new iteration, the "state" of the previous iteration is carried forward to the new iteration but we cannot return to a previous state. With recursion we can return to a previous state because each recursion invokes a new instance of the function which automatically saves the state of the current instance. Whenever we return from an instance, the result of that instance can be passed back to the previous instance and be incorporated into its restored state.To demonstrate, consider the following iterative loop (in C++):for (unsigned i=0; i