Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Recursion and Performance

Tags:

java

recursion

Does the recursion heavily impose on processor and ram? I mean, that one of my threads has a method, that is very likely to call itself. Let's say that It can self-call about one time per second. My app should run for at least 24 hours without stopping, so it gives (60*60*24) 86400 self-called methods.

How does it influence on the second (main) thread?

Sorry for my bad english, and for no code, but im not writing from home.

like image 708
noisy cat Avatar asked Aug 07 '12 20:08

noisy cat


People also ask

Is recursion faster than iteration Java?

The recursive function runs much faster than the iterative one. The reason is because in the latter, for each item, a CALL to the function st_push is needed and then another to st_pop . In the former, you only have the recursive CALL for each node.

Is recursion fast or slow?

Although recursive methods run slower, they sometimes use less lines of code than iteration and for many are easier to understand. Recursive methods are useful for certain specific tasks, as well, such as traversing tree structures.

How do you improve recursion performance?

Bottom-up. Sometimes the best way to improve the efficiency of a recursive algorithm is to not use recursion at all. In the case of generating Fibonacci numbers, an iterative technique called the bottom-up approach can save us both time and space.

Does recursion increase time complexity?

Recursion: Recursion has the overhead of repeated function calls, that is due to repetitive calling of the same function, the time complexity of the code increases manyfold.


2 Answers

If there are no return statements which will end the string of recursive calls before the 86400th call, it is likely that you will have a stack overflow error from there being too many recursive calls on the stack. Try implementing an iterative solution if possible.

like image 69
shazeline Avatar answered Nov 15 '22 00:11

shazeline


Recursive calls are very inefficient in terms of memory.
This is because each recursive call adds a new frame to the stack and so for N calls you have O(N) memory requirements.
Recursive methods solve difficult problems in a really simple way (e.g. traversal of a tree) with simple code.
The downside is that if you don't know what you are doing you could get run out of memory as a result of too many recursive calls.
So if you know that a problem can be solved recursivelly but you need too much recursion try to implement it iteratively (most but not all recursive algorithms can be transformed to iterative ones)

Example. In my Windows 32-bit (4GB) the following gets a Exception in thread "main" java.lang.StackOverflowError after 7380 calls

public static void recursing( int n ){
        System.out.println(n++);
        recursing(n);
}  
public static void main(String[] args) {
    recursing(1);

}
like image 37
Cratylus Avatar answered Nov 14 '22 22:11

Cratylus