-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeDiff.py
executable file
·72 lines (61 loc) · 1.9 KB
/
timeDiff.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
# Write your list_stats function here.
import math;
import numpy as np
import time
import statistics
import matplotlib.pyplot as plt
def list_stats_1 (arr) :
size = len(arr)
arr.sort()
mid = math.floor(size/2)
mean = sum(arr)/size if size > 0 else 0
median = 0
if size > 0:
if size % 2 == 0:
median = (arr[mid-1]+arr[mid])/2
else:
median = arr[mid]
return np.round(median, 5), np.round(mean, 5)
def list_stats_2 (arr) :
mean = np.mean(arr)
median = np.median(arr)
return np.round(median, 5), np.round(mean, 5)
def list_stats_3 (arr) :
mean = statistics.mean(arr)
median = statistics.median(arr)
return np.round(median, 5), np.round(mean, 5)
# You can use this to test your function.
# Any code inside this `if` statement will be ignored by the automarker.
if __name__ == '__main__':
# Run your function with the first example in the question.
rang = np.arange(1, 8)
print(rang)
normal = []
numpyS = []
stats = []
for i in rang:
arr = np.random.randn(10**i)
start = time.perf_counter()
m = list_stats_1(arr)
end = time.perf_counter() - start
normal.append(end)
start = time.perf_counter()
m = list_stats_2(arr)
end = time.perf_counter() - start
numpyS.append(end)
start = time.perf_counter()
m = list_stats_3(arr)
end = time.perf_counter() - start
stats.append(end)
plt.plot(rang, normal, label = 'Inbuilt functions')
plt.plot(rang, numpyS, label = 'numpy stats')
plt.plot(rang, stats, label = 'Statistics import')
plt.xlabel('x - axis')
plt.ylabel('y - axis')
plt.legend()
plt.show()
a = np.zeros(5, dtype=np.int32)
b = np.zeros(5, dtype=np.float64)
for obj in [a, b]:
print('nbytes :', obj.nbytes)
print('size x itemsize:', obj.size*obj.itemsize)