- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
Latest commit
196 lines (127 loc) · 6.66 KB
/
Copy pathmain.py
File metadata and controls
196 lines (127 loc) · 6.66 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
importtime
fromhwcounterimportTimer
importmatplotlib.pyplotasplt
importnumpyasnp
fromscipyimportstats
fromtests.string_comparisonimporttest_compare_int, test_compare_string, test_compare_string_long, test_compare_float
fromtests.shapesimporttest_shape_monomorphic, test_shape_polymorphic, test_shape_megamorphic
fromtests.functional_methodsimporttest_functional_numbers, test_imperative_numbers
fromtests.indirectionimporttest_class_access_getattr, test_class_access_getitem, test_map_access, test_direct_access
fromtests.sequental_random_accessimporttest_sequential_access, test_random_access
fromtests.cache_accessimporttest_l1, test_l2, test_l3, test_ram
fromtests.large_objimporttest_large_obj_direct, test_large_obj_indirect
fromtests.eval_dynamic_keyimporttest_with_eval, test_without_eval
fromtests.string_mutation_concatenationimporttest_string_mutation, test_string_concatenation
fromtests.specializedimporttest_not_specialized, test_specialized
fromtests.comprehensionsimporttest_comprehensions, test_regular_loops
fromtests.float_int_arthimeticimporttest_float_arithmetic, test_int_arithmetic
fromtests.data_structuresimporttest_list, test_set
fromtests.numpyimporttest_regular_float_sum, test_numpy_float_sum, test_builtin_float_sum
fromtests.numpy_with_array_typeimporttest_regular_float_sum_with_array, test_numpy_float_sum_with_array, test_builtin_float_sum_with_array
fromtests.unrollingimporttest_sum_list_unrolled_0, test_sum_list_unrolled_2, test_sum_list_unrolled_4, test_sum_list_unrolled_8, test_sum_list_unrolled_16
fromtests.unrolling_cythonimporttest_cython_sum_list_unrolled_0, test_cython_sum_list_unrolled_2, test_cython_sum_list_unrolled_4, test_cython_sum_list_unrolled_8, test_cython_sum_list_unrolled_16
tests= [
# test_compare_int, test_compare_string, test_compare_string_long, test_compare_float
# test_shape_monomorphic, test_shape_polymorphic, test_shape_megamorphic,
# test_functional_numbers, test_imperative_numbers,
# test_class_access_getattr, test_class_access_getitem, test_map_access, test_direct_access,
# test_sequential_access, test_random_access,
# test_l1, test_l2, test_l3, test_ram,
# test_large_obj_indirect, test_large_obj_direct
# test_with_eval, test_without_eval,
# test_string_mutation, test_string_concatenation,
# test_not_specialized, test_specialized,
# test_list, test_set,
# test_regular_loops, test_comprehensions,
# test_float_arithmetic, test_int_arithmetic,
# test_regular_float_sum, test_numpy_float_sum, test_builtin_float_sum,
# test_regular_float_sum_with_array, test_numpy_float_sum_with_array, test_builtin_float_sum_with_array,
# test_sum_list_unrolled_0, test_sum_list_unrolled_2, test_sum_list_unrolled_4, test_sum_list_unrolled_8, test_sum_list_unrolled_16,
test_cython_sum_list_unrolled_0, test_cython_sum_list_unrolled_2, test_cython_sum_list_unrolled_4, test_cython_sum_list_unrolled_8, test_cython_sum_list_unrolled_16
]
withTimer() ast:
time.sleep(1)
cycles_per_second=t.cycles
print("Running tests...")
sizes= [10**iforiinrange(1, 7)] # 10, 100, 1000... 1 million
runs=30
cycles_per_test= []
foriinrange(len(sizes)):
size_dimension= []
cycles_per_test.append(size_dimension)
forjinrange(len(tests)):
test_dimension= []
size_dimension.append(test_dimension)
total_cycles_per_test= []
foriinrange(len(sizes)):
total_cycles_per_test.append([0] *len(tests))
fors, sizeinenumerate(sizes):
print(f"\n\nSize: {size}")
foriinrange(0, runs):
print(f"\nRun {str(i)}:")
fort, testinenumerate(tests):
withTimer() astimer:
test(size)
cycles=timer.cycles
print(f'{test.__name__}: cycles = {cycles}, time taken = {cycles/cycles_per_second*1000} ms')
cycles_per_test[s][t].append(cycles)
total_cycles_per_test[s][t] +=cycles
# Perform statistical tests
print("\nStatistical Significance")
fors, sizeinenumerate(sizes):
print("\nSize", size)
foriinrange(0, len(tests) -1):
forjinrange(i+1, len(tests)):
test1_cycles=cycles_per_test[s][i]
test2_cycles=cycles_per_test[s][j]
print(f"{tests[i].__name__} cycles=", test1_cycles)
print(f"{tests[j].__name__} cycles=", test2_cycles)
t_stat, p_value=stats.ttest_ind(test1_cycles, test2_cycles, equal_var=False)
print(f"t-statistic: {t_stat}, p-value: {p_value}")
ifp_value<0.05:
print("There is a statistically significant difference between the two tests.")
else:
print("No statistically significant difference was found between the two tests.")
print() # new line
print("\nSize, "+', '.join([test.__name__fortestintests]))
avg_cycles= []
time_taken_ms= []
fors, sizeinenumerate(sizes):
avg_cycles_row= []
time_taken_row= []
foridx, testinenumerate(tests):
avg_cycle=total_cycles_per_test[s][idx] /runs
time_taken=avg_cycle/cycles_per_second*1000
avg_cycles_row.append(avg_cycle)
time_taken_row.append(time_taken)
print(f'{size}, '+', '.join(map(lambdax: str(x), time_taken_row)))
avg_cycles.append(avg_cycles_row)
time_taken_ms.append(time_taken_row)
## Bar Graphs ##
# Number of sizes
# n_sizes = len(sizes)
# # Create a subplot for each size
# fig, axs = plt.subplots(n_sizes, 1, figsize=(10, 2 * n_sizes))
# for idx, size in enumerate(sizes):
# axs[idx].bar(range(len(tests)), time_taken_ms[idx], tick_label=[test.__name__ for test in tests])
# axs[idx].set_ylim(0, max(time_taken_ms[idx]))
# axs[idx].set_ylabel('Time (ms)')
# axs[idx].set_title(f'Size: {size}')
# axs[idx].grid(True, axis='y', ls="--")
# plt.tight_layout()
# plt.show()
## Line Graph ##
# Plotting the data with discrete points for each test and size
plt.figure(figsize=(12, 6))
# Plot each test with markers and annotate each point with the exact time
foridx, testinenumerate(tests):
times= [row[idx] forrowintime_taken_ms]
plt.plot(sizes, times, marker='o', label=test.__name__)
fori, timeinenumerate(times):
plt.annotate(f'{time:.3f}', (sizes[i], time), textcoords="offset points", xytext=(0,8), ha='center', rotation=45)
plt.xscale('log')
plt.yscale('log')
plt.xlabel('Size')
plt.ylabel('Time Taken (ms)')
plt.legend()
plt.show()