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))
3 ответов
+ 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..
+ 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.
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 ..