-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacci.py
63 lines (40 loc) · 1.27 KB
/
fibonacci.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*-
import time
def fib1(n):
"""Fib with recursion."""
# base case
if n==0 or n==1:
return 1
# recurssive caae
else:
return fib1(n-1) + fib1(n-2)
def fib2(n):
"""Fib without recursion."""
a, b = 0, 1
for i in range(1, n+1):
a, b = b, a+b
return b
def fib3(n, cache = {0: 1, 1: 1}):
"""Fib with recursion and caching."""
try:
return cache[n]
except KeyError:
cache[n] = fib3(n-1) + fib3(n-2)
return cache[n]
if __name__ == '__main__':
test_n = 30
start_time = time.time()
print(fib1(test_n))
exec_time = time.time() - start_time
print("运行函数fib1耗时:%.4f秒!!!"%(exec_time))
# 运行函数fib1耗时:0.3989秒!!!
start_time = time.time()
print(fib2(test_n))
exec_time = time.time() - start_time
print("运行函数fib2耗时:%.4f秒!!!"%(exec_time))
# 运行函数fib2耗时:0.0000秒!!!
start_time = time.time()
print(fib3(test_n))
exec_time = time.time() - start_time
print("运行函数fib3耗时:%.4f秒!!!"%(exec_time))
# 运行函数fib2耗时:0.0000秒!!!