-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfibCaseStudy.py
More file actions
64 lines (49 loc) · 1.19 KB
/
fibCaseStudy.py
File metadata and controls
64 lines (49 loc) · 1.19 KB
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
#
# Fib Casestudy Example
#
import matplotlib.pyplot as plt
from_n, to_n = 1, 30
X = range(from_n, to_n)
#print(X)
# our old fib function
def fibslow(n):
if n == 0 or n == 1 :
return n
else:
return fibslow(n-1) + fibslow(n-2)
def fibfast(n):
if n <= 1: return n
a, b = 0, 1
for i in range(1, n+1):
a, b = b, a + b
return a
#
# Time Calculate function
#
def time_function(fn, start, end):
from datetime import datetime
times = []
for i in range(start, end):
start_time = datetime.now()
_ = fn(i) # actual call to the function, don't care about value
end_time = datetime.now()
time_taken = end_time - start_time # returns a timedelta
time_taken = time_taken.microseconds
times.append(time_taken)
return times
#
# Fib slow time
#
fibslow_times = time_function(fibslow, from_n, to_n)
fibfast_times = time_function(fibfast, from_n, to_n)
print(fibslow_times)
print(fibfast_times)
#
# Ploting fibSlow and fibFast time graph
#
plt.figure()
plt.plot(X, fibslow_times, label='fibslow')
plt.plot(X, fibfast_times, label='fibfast')
plt.xlabel('n')
plt.ylabel('time (microseconds)')
plt.legend()