-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathperformance.py
73 lines (54 loc) · 1.5 KB
/
performance.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
64
65
66
67
68
69
70
71
72
73
# coding: utf-8
"""
Performance comparison of different method to finding Fibonacci numbers
https://en.wikipedia.org/wiki/Fibonacci_number
"""
import sys
from time import time
from functools import lru_cache
from functools import wraps
import numpy as np
def measure(func):
@wraps(func)
def _time_it(*args, **kwargs):
start = int(round(time() * 1000))
value = func(*args, **kwargs)
end_ = int(round(time() * 1000)) - start
return value, end_
return _time_it
@lru_cache(maxsize=None)
def fib(n):
if n < 2:
return n
return fib(n-1) + fib(n-2)
@measure
def fib_dynamic(n):
a = 0
b = 1
for __ in range(n):
a, b = b, a + b
return a
@measure
def fib_cache(n):
return fib(n)
@measure
def fib_matrix(n):
qmatrix = np.matrix([[1, 1], [1, 0]], dtype=object)
return (qmatrix ** n)[0, 1]
@measure
def fib_golden_ratio(n):
evals = [(1+np.sqrt(5))/2, (1-np.sqrt(5))/2]
return int((evals[0]**n - evals[1]**n) / np.sqrt(5))
if __name__ in '__main__':
sys.setrecursionlimit(40000)
for func, fnum_list in [
(fib_golden_ratio, [1e2, 1e3]),
(fib_cache, [1e2, 1e3, 1e4]),
(fib_dynamic, [1e2, 1e3, 1e4, 1e5, 1e6]),
(fib_matrix, [1e2, 1e3, 1e4, 1e5, 1e6])
]:
print('\nFunction {}'.format(func.__name__))
for fnumber in fnum_list:
value, evaltime = func(int(fnumber))
print(('{:7d} fibonacci number: {} ms'
.format(int(fnumber), evaltime)))