+ 1
How come recursion is faster?
memoization doesn't calculate the value again but recursuon does. so how come in my code recursion is faster? https://code.sololearn.com/cPB8Rh4injLr/?ref=app is my implementation wrong?
4 Answers
+ 3
That's because you don't run the function 100 times, BUT the string containing the function, including the line "fact = {}" which resets the dictionary at every iteration. This makes recursion slightly faster as here memorization would be the same but with memory operations added.
Just take out "fact = {}" and it will work fine.
+ 1
OrHy3 thank you
+ 1
Harsha S
Maybe you are also interested in this thread:
https://www.sololearn.com/discuss/3028941/?ref=app
Tibor added some nice memoization examples using lru_cache.
+ 1
Denise RoĂberg thank you