After some research about recursive functions i am facing contradiction: On one side solving problems in recursive way is elegant while on the other side in practice performance seems to be horrible and number of recursive calls is limited.
I understand by default Pythons recursive depth is limited to 1000, however even in a simple application i get very bad performmance as early as 40 - 50 calls.
Let me give an example:
def fibonacci(n):
if n == 1 or n == 0:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
This simple recursive function on my PC takes huge amount of time to solve even for low n. For testing i also wrote another function:
def fib_nonrecursive(n):
fib_seq = [1, 1]
for i in range(2, n+1):
value = fib_seq[i-1] + fib_seq[i-2]
fib_seq.append(value)
return fib_seq[i]
Non recursive way is very fast even on big numbers, so definitivly problem cannot be operations involved or size of numbers. So my question is why the recursive way is so slow and is there any way to make it faster? Is there any way to expand resursive depth?
EDIT Since answers suggested using memoization i looked into it and implemented it on my example:
def mem(f):
memory = {}
def inner_function(x):
if x not in memory:
memory[x] = f(x)
return memory[x]
else:
return memory[x]
return inner_function
@mem
def fibonacci(n):
if n == 1 or n == 0:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
Same mem(f)
can be used with other recursive functions f
. The @mem
part must be included for f
to be passed as argument to mem()
(see "decorators")
It is slightly advanced way to code but i didnt find an easier was to implement memoization for given example. If there is simpler way of implementation pls correct me.