Anything after a return
statement will never be executed. A return statement instantly terminates the function and "returns" to the caller. So it's really not possible.
There are two ways to get (nearly) the same result though.
Method #1:
The time it takes to execute a return statement is almost negligable. Therefore you can move the necessary benchmark code above the return and get an almost identical result as if it actually executed after a 'return' statement.
def F(n):
t=time.time()
if n==0:
return (0)
elif n==1:
return (1)
else:
return (F(n-1)+F(n-2))
t1==time.time()
F_time==t1-t
print ('It took',F_time,'seconds to sort',n,'values using recursion')
return t
Method #2:
Put your timing code outside the function, this is the preferred method.
def F(n):
if n==0:
return (0)
elif n==1:
return (1)
else:
return (F(n-1)+F(n-2))
return t
t=time.time()
F(5)
t1==time.time()
F_time==t1-t
print ('It took',F_time,'seconds to sort',5,'values using recursion')
Hope this helped, good luck!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…