Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Python: Is there a difference between calling multiple recursions in one line or in multiple lines?

Is there a difference between calling two or more recursive calls in one line or in different lines? here are two basic examples:

def getFib(n):
    if n == 1 or n == 2:
        return 1
    return getFib(n-1) + getFib(n-2)

Are those two calls run simultaneously? or it goes through one of them then goes for the other one?

def hanoi(n, s, t, b):
    assert n > 0
    if n ==1:
        print 'move ', s, ' to ', t
    else:
        hanoi(n-1,s,b,t)
        hanoi(1,s,t,b)
        hanoi(n-1,b,t,s)

same question for this example.

Update

Since my question has marked as duplicate of (the executed order of recursion function in python2.7 I ask it in this way to clarify the difference: is there a difference between efficiency of calling multiple recursive calls in one line or in multiple lines? is one of them faster than the other one?

for example is there a difference between the above mentioned Fibonacci function and this one?

def getFib2(n):
    if n == 1 or n == 2:
        return 1
    a = getFib2(n-1)
    b = getFib2(n-2)
    c = a + b 
    return c 

And in case calling recursions in one line is faster how can I run multiple recursions at once?

Update2

Consider the case below:

import timeit
n = 43
tic = timeit.default_timer()
print (getFib(n))
toc = timeit.default_timer()
print (toc - tic, ' Seconds')

tic2 = timeit.default_timer()
print (getFib2(n))
toc2 = timeit.default_timer()
print (toc2 - tic2, ' Seconds')

433494437
130.47638419398572  Seconds
433494437
141.92620811692905  Seconds

Can you please explain why it takes more time in the second case?

Comments