Recursion vs For loops - Factorials, Java?

The for loop will be more efficient because there is no overhead of the method calls. (As a general rule loops are almost always more efficient than recursion) To explain why you have to get into the nitty gritty details of what happens when you call a method and something called the call stack Basically when you call a method it needs a bit of space to work with (for things like its local variables etc) it also needs space for the incoming parameters being passed to it and a place to store the return address of where it should go when it is done executing. This space is dynamically provided by 'pushing' the values on to a stack.

That stack space remains occupied until the method returns at which point it is 'popped off So think about recursion in this context: every time you call the method from within itself you push more data onto the stack, without returning from the method you were in (and thus without freeing up the stack space the calling method was occupying). As your recursion goes deeper the amount of memory increases In contrast the for loop you wrote just uses the fixed amount of memory provided by one stack frame push.

The for loop will be more efficient because there is no overhead of the method calls. (As a general rule loops are almost always more efficient than recursion) To explain why you have to get into the nitty gritty details of what happens when you call a method and something called the call stack. Basically when you call a method it needs a bit of space to work with (for things like its local variables etc) it also needs space for the incoming parameters being passed to it and a place to store the return address of where it should go when it is done executing.

This space is dynamically provided by 'pushing' the values on to a stack. That stack space remains occupied until the method returns at which point it is 'popped off'. So think about recursion in this context: every time you call the method from within itself you push more data onto the stack, without returning from the method you were in (and thus without freeing up the stack space the calling method was occupying).

As your recursion goes deeper the amount of memory increases. In contrast the for loop you wrote just uses the fixed amount of memory provided by one stack frame push.

Of course unless you use some functional language and profit from tail recursion. – binary_runner Sep 24 at 9:21 A call on long will overrun with factorial (21), which is a small number, to get a significant performance problem. For higher numbers, the code will fail - for very frequent calls, a hashmap will be faster than repeated loops.

Multiple calls on BigInteger might still be faster with a hashmap behind the scene. Don't forget: Premature optimization is the root of all evil. – user unknown Sep 24 at 9:37 Yes yes you all are raising extremely valid points, but I also think that you are over engineering things a bit.

I suspect Deley isn't really looking to find the best way to calculate factorials but really just looking for some help with his homework... – Matthew Sep 24 at 9:41 The homework tag seems to been removed, meanwhile. – user unknown Sep 24 at 14:39.

The only problem with recursion is that every time that the function is called it's address is put on the top of the stack, concatenating a lot of call can cause a stack overflow exception if the number of recursion calls is high .

With 21 calls, your long will overrun, which is very early to measure a difference, which could get relevant. You might need BigInteger to measure a significant difference, when measuring factorial (10*1000*1000) or something that large. If you don't have high numbers to calculate, but calculate low factorials often, a hashmap with precalculated values will be faster than recursion and looping.

1: I would say; check that performance matters before worrying about it. – Peter Lawrey Sep 24 at 10:53 Yes, that's the other mantra when talking about performance. – user unknown Sep 24 at 14:35.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions