-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmarks.py
288 lines (243 loc) · 10.6 KB
/
benchmarks.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import random
import time
from benchmark_versions.benchmark_merge_sort import merge_sort
from benchmark_versions.benchmark_natural_merge_sort import natural_merge_sort
from benchmark_versions.benchmark_timsort import timsort
from benchmark_versions.benchmark_powersort import powersort
from output_generation import plot_results, plot_minrun_results, save_to_csv
from random_input_generators import generate_random_list
def timeit(func):
def wrapper(*args, **kwargs):
start_time = time.process_time()
result = func(*args, **kwargs)
time_taken = time.process_time() - start_time
return result, time_taken * 1000
return wrapper
class Comparable:
# Static variable to track the number of comparisons
comparison_count = 0
def __init__(self, value):
self.value = value
def __lt__(self, other):
Comparable.comparison_count += 1
return self.value < other.value
def __le__(self, other):
Comparable.comparison_count += 1
return self.value <= other.value
def __gt__(self, other):
Comparable.comparison_count += 1
return self.value > other.value
def __ge__(self, other):
Comparable.comparison_count += 1
return self.value >= other.value
def __eq__(self, other):
Comparable.comparison_count += 1
return self.value == other.value
def __ne__(self, other):
Comparable.comparison_count += 1
return self.value != other.value
def run_python_sort_for_comparisons(arr):
Comparable.comparison_count = 0
wrapped_arr = [Comparable(x) for x in arr]
sorted(wrapped_arr)
return Comparable.comparison_count
@timeit
def run_timsort(arr):
return timsort(arr)
@timeit
def run_powersort(arr, fix_minrun=True):
return powersort(arr, fix_minrun)
@timeit
def run_natural_merge_sort(arr):
return natural_merge_sort(arr)
@timeit
def run_merge_sort(arr):
return merge_sort(arr)
@timeit
def run_python_sort(arr):
return sorted(arr)
PROD_N_SAMPLES = 10 # TODO more
PROD_SIZE_CONFIGURATIONS = [
[n for n in range(100, 1000, 100)],
[n for n in range(1000, 10_000, 1000)],
[n for n in range(10_000, 100_000, 10_000)],
[n for n in range(100_000, 1_000_001, 100_000)]
]
PROD_RUNS_CONFIGURATIONS = {
# TODO explain these values
# TODO what if I say there will be: N/2 (random), 1000 (presorted), 10 (heavily_presorted) runs?
"random": 2,
"presorted": 50,
"heavily_presorted": 500,
}
PROD_ENTROPY_CONFIGURATIONS = {
# TODO it's important to understand what distribution does this have, in other to define these categories
# TODO is this a normal distribution? actually, I think it's converging toward 1.
"very_skewed": (.1, .2), # 10-20% of logK
"partially_uniform": (.4, .6), # 40-60% of logK
"heavily_uniform": (.9, 1.), # 90-100% of logK
}
N_SAMPLES = 10
SIZE_CONFIGURATIONS = [
# [n for n in range(10, 100, 10)],
[n for n in range(100, 1000, 100)],
[n for n in range(1000, 10_000, 1000)],
[n for n in range(10_000, 100_000, 10_000)],
# [n for n in range(100_000, 1_000_001, 100_000)]
]
RUNS_CONFIGURATIONS = {
"random": 2,
"presorted": 50,
"heavily_presorted": 500,
}
ENTROPY_CONFIGURATIONS = {
"very_skewed": (.1, .2), # 10-20% of logK
"partially_uniform": (.4, .6), # 40-60% of logK
"heavily_uniform": (.9, 1.), # 90-100% of logK
}
# arr_size vs. comparisons with and without minrun + insertion sorting
def benchmark_minrun_impact():
results = {}
for arr_sizes in SIZE_CONFIGURATIONS:
for arr_size in arr_sizes:
print(arr_size)
bounds = (0, arr_size*10) # TODO?
sum_with = 0
for _ in range(N_SAMPLES):
arr = generate_random_list(arr_size, bounds)
(_, n_with), _ = run_powersort(arr.copy(), fix_minrun=True)
(_, n_without), _ = run_powersort(arr.copy(), fix_minrun=False)
sum_with += (n_with-n_without)/n_without
results[arr_size] = (0, sum_with/N_SAMPLES)
plot_minrun_results(results, "Array size", "# of key comparisons [% diff]",
"Performance impact of MIN_RUN and using insertion sort for small runs",
"minrun_impact_comparisons", xlog=True)
return results
def benchmark_random():
results = {}
for arr_sizes in SIZE_CONFIGURATIONS:
for arr_size in arr_sizes:
print(arr_size)
bounds = (0, arr_size*10) # TODO?
sum_merge_sort = 0
sum_natural_merge_sort = 0
sum_timsort = 0
sum_powersort = 0
sum_python_sort = 0
for _ in range(N_SAMPLES):
arr = generate_random_list(arr_size, bounds)
(_, n_merge_sort), _ = run_merge_sort(arr.copy())
(_, n_natural_merge_sort), _ = run_natural_merge_sort(arr.copy())
(_, n_timsort), _ = run_timsort(arr.copy())
(_, n_powersort), _ = run_powersort(arr.copy())
n_python_sort = run_python_sort_for_comparisons(arr.copy())
delta = lambda n: (n-n_merge_sort)/n_merge_sort
sum_merge_sort += delta(n_merge_sort)
sum_natural_merge_sort += delta(n_natural_merge_sort)
sum_timsort += delta(n_timsort)
sum_powersort += delta(n_powersort)
sum_python_sort += delta(n_python_sort)
results[arr_size] = (sum_merge_sort/N_SAMPLES,
sum_natural_merge_sort/N_SAMPLES,
sum_timsort/N_SAMPLES,
sum_powersort/N_SAMPLES,
sum_python_sort/N_SAMPLES)
file_name = "benchmark_random"
save_to_csv(results, file_name)
plot_results(results, "Array size (N)", "# of key comparisons [% diff from Merge Sort]",
f"Array size vs. # of key comparisons",
file_name, fit_to_poly=True, show=False)
return results
def benchmark_runs():
for config_name, factor in RUNS_CONFIGURATIONS.items():
_benchmark_runs(config_name, factor)
def _benchmark_runs(config_name, factor):
results = {}
for arr_sizes in SIZE_CONFIGURATIONS:
for arr_size in arr_sizes:
print(arr_size)
bounds = (0, arr_size * 10) # TODO?
n_runs = arr_size // factor
if not n_runs:
continue
sum_merge_sort = 0
sum_natural_merge_sort = 0
sum_timsort = 0
sum_powersort = 0
sum_python_sort = 0
for _ in range(N_SAMPLES):
arr = generate_random_list(arr_size, bounds, number_of_runs=n_runs)
(_, n_merge_sort), _ = run_merge_sort(arr.copy())
(_, n_natural_merge_sort), _ = run_natural_merge_sort(arr.copy())
(_, n_timsort), _ = run_timsort(arr.copy())
(_, n_powersort), _ = run_powersort(arr.copy())
n_python_sort = run_python_sort_for_comparisons(arr.copy())
delta = lambda n: (n - n_merge_sort) / n_merge_sort
sum_merge_sort += delta(n_merge_sort)
sum_natural_merge_sort += delta(n_natural_merge_sort)
sum_timsort += delta(n_timsort)
sum_powersort += delta(n_powersort)
sum_python_sort += delta(n_python_sort)
results[arr_size] = (sum_merge_sort / N_SAMPLES,
sum_natural_merge_sort / N_SAMPLES,
sum_timsort / N_SAMPLES,
sum_powersort / N_SAMPLES,
sum_python_sort / N_SAMPLES)
file_name = f"benchmark_runs_{config_name}"
save_to_csv(results, file_name)
plot_results(results, "Array size (N)", "# of key comparisons [% diff from Merge Sort]",
f"Array size vs. # of key comparisons (number of runs is N/{factor} => array is {config_name})",
file_name, fit_to_poly=True, show=False)
return results
def benchmark_entropy():
for config_name, entropy_interval in ENTROPY_CONFIGURATIONS.items():
_benchmark_entropy(config_name, entropy_interval)
def _benchmark_entropy(config_name, entropy_interval):
"""
TODO the problem is that the algorithms work with a different run profile (because of MIN_RUN=32)
So results from this might actually be kinda useless, unless the runs are really big?
=> I think this is in fact not a problem, but let's see on results
"""
entropy_from, entropy_to = entropy_interval
results = {}
for arr_sizes in SIZE_CONFIGURATIONS:
for arr_size in arr_sizes:
print(arr_size)
bounds = (0, arr_size * 10) # TODO?
sum_merge_sort = 0
sum_natural_merge_sort = 0
sum_timsort = 0
sum_powersort = 0
sum_python_sort = 0
for _ in range(N_SAMPLES):
arr = generate_random_list(arr_size, bounds, entropy_range=(entropy_from, entropy_to))
(_, n_merge_sort), _ = run_merge_sort(arr.copy())
(_, n_natural_merge_sort), _ = run_natural_merge_sort(arr.copy())
(_, n_timsort), _ = run_timsort(arr.copy())
(_, n_powersort), _ = run_powersort(arr.copy())
n_python_sort = run_python_sort_for_comparisons(arr.copy())
delta = lambda n: (n - n_merge_sort) / n_merge_sort
sum_merge_sort += delta(n_merge_sort)
sum_natural_merge_sort += delta(n_natural_merge_sort)
sum_timsort += delta(n_timsort)
sum_powersort += delta(n_powersort)
sum_python_sort += delta(n_python_sort)
results[arr_size] = (sum_merge_sort / N_SAMPLES,
sum_natural_merge_sort / N_SAMPLES,
sum_timsort / N_SAMPLES,
sum_powersort / N_SAMPLES,
sum_python_sort / N_SAMPLES)
file_name = f"benchmark_entropy_{config_name}"
save_to_csv(results, file_name)
plot_results(results, "Array size (N)", "# of key comparisons [% diff from Merge Sort]",
f"Array size vs. # of key comparisons (entropy interval is "
f"{entropy_interval[0]*100}%-{entropy_interval[1]*100}% => run profile is {config_name})",
file_name, fit_to_poly=True, show=False)
return results
def run_all_benchmarks():
benchmark_minrun_impact()
# benchmark_random()
# benchmark_runs()
# benchmark_entropy()
if __name__ == '__main__':
run_all_benchmarks()