0

Tell me, is there a difference in performing 2 types of recursion? Which one will run faster?

def factorial_recursive(n): if n != 1: return n*factorial_recursive(n-1) else: return n print(factorial_recursive(5)) # Or def factorial_recursive(n): if n == 1: return n else: return n*factorial_recursive(n-1) print(factorial_recursive(5))

19th Jun 2021, 11:47 AM
Тимур Завьялов
Тимур Завьялов - avatar
3 Réponses
+ 3
There is no difference. But according to flow of execution, when if part is false, then only else part will go for execution otherwise not. So may 1st will execute less lines of code than 2nd one..
19th Jun 2021, 11:56 AM
Jayakrishna 🇮🇳
+ 3
Тимур Завьялов , when running both versions in debug mode, both will execute the same number of lines / steps. taking this into account, it should not matter which version is used. but may be it is helpful to check the execution time with a profiler.
19th Jun 2021, 12:57 PM
Lothar
Lothar - avatar
0
Interestingly debugging with time difference, iam getting 2nd one taking less time. So I may be wrong...? Or may be it's because of recursions? can a python expert answer here ..
19th Jun 2021, 1:30 PM
Jayakrishna 🇮🇳