+ 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?

12th May 2022, 1:03 PM
Harsha S
Harsha S - avatar
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.
12th May 2022, 1:43 PM
OrHy3
OrHy3 - avatar
+ 1
OrHy3 thank you
12th May 2022, 2:00 PM
Harsha S
Harsha S - avatar
+ 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.
12th May 2022, 9:53 PM
Denise RoƟberg
Denise RoƟberg - avatar
+ 1
13th May 2022, 2:33 AM
Harsha S
Harsha S - avatar