Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import time
- def run(func,arg):
- """
- Calculate the execution time
- """
- start = time()
- ans = func(arg)
- end = time()
- long = (end-start)*1000
- print(f"{func.__name__}({arg}) take {long} ms to execute")
- print(f"Output: {ans}")
- # Recursive Function
- def re_f(n):
- if n == 0 or n == 1:
- return 1
- else:
- return re_f(n-1) + re_f(n-2)
- # Dynamic Programming
- def dyn_f(n, dic = {}):
- if n in dic:
- return dic[n]
- if n == 0 or n == 1:
- return 1
- else:
- dic[n] = dyn_f(n-1,) + dyn_f(n-2)
- return dic[n]
- run(dyn_f,30)
- run(re_f,30)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement