forked from swenson/sort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.c
301 lines (254 loc) · 7.42 KB
/
demo.c
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
289
290
291
292
293
294
295
296
297
298
299
300
301
/* Copyright (c) 2010-2014 Christopher Swenson. */
/* Copyright (c) 2012 Google Inc. All Rights Reserved. */
#define _XOPEN_SOURCE
#include <sys/time.h>
#define SORT_NAME sorter
#define SORT_TYPE int64_t
/* You can redefine the comparison operator.
The default is
#define SORT_CMP(x, y) ((x) < (y) ? -1 : ((x) == (y) ? 0 : 1))
but the one below is often faster for integer types.
*/
#define SORT_CMP(x, y) (x - y)
#include "sort.h"
/*
We now have the following functions defined
* sorter_shell_sort
* sorter_binary_insertion_sort
* sorter_heap_sort
* sorter_quick_sort
* sorter_merge_sort
* sorter_selection_sort
* sorter_tim_sort
Each takes two arguments: int64_t *array, size_t size
*/
/* Used to control the demo */
#define SEED 123
#define SIZE 10000
#define RUNS 1
/* helper functions */
void verify(int64_t *dst, const int size) {
int i;
for (i = 1; i < size; i++) {
if (dst[i - 1] > dst[i]) {
printf("Verify failed! at %d\n", i);
for (i = i - 2; i < SIZE; i++) {
printf(" %lld", (long long int)dst[i]);
}
printf("\n");
break;
}
}
}
static __inline double utime() {
struct timeval t;
gettimeofday(&t, NULL);
return (1000000.0 * t.tv_sec + t.tv_usec);
}
static void fill(int64_t *arr, const int size) {
int i;
for (i = 0; i < size; i++) {
arr[i] = lrand48();
}
}
/* used for stdlib */
static __inline int simple_cmp(const void *a, const void *b) {
const int64_t da = *((const int64_t *) a);
const int64_t db = *((const int64_t *) b);
return (da < db) ? -1 : (da == db) ? 0 : 1;
}
void run_tests(void) {
int i;
int64_t arr[SIZE];
int64_t dst[SIZE];
double start_time;
double end_time;
double total_time;
printf("Running tests\n");
srand48(SEED);
total_time = 0.0;
for (i = 0; i < RUNS; i++) {
fill(arr, SIZE);
memcpy(dst, arr, sizeof(int64_t) * SIZE);
start_time = utime();
qsort(dst, SIZE, sizeof(int64_t), simple_cmp);
end_time = utime();
total_time += end_time - start_time;
verify(dst, SIZE);
}
printf("stdlib qsort time: %10.2f us per iteration\n", total_time / RUNS);
#ifndef __linux__
srand48(SEED);
total_time = 0.0;
for (i = 0; i < RUNS; i++) {
fill(arr, SIZE);
memcpy(dst, arr, sizeof(int64_t) * SIZE);
start_time = utime();
heapsort(dst, SIZE, sizeof(int64_t), simple_cmp);
end_time = utime();
total_time += end_time - start_time;
verify(dst, SIZE);
}
printf("stdlib heapsort time: %10.2f us per iteration\n", total_time / RUNS);
srand48(SEED);
total_time = 0.0;
for (i = 0; i < RUNS; i++) {
fill(arr, SIZE);
memcpy(dst, arr, sizeof(int64_t) * SIZE);
start_time = utime();
mergesort(dst, SIZE, sizeof(int64_t), simple_cmp);
end_time = utime();
total_time += end_time - start_time;
verify(dst, SIZE);
}
printf("stdlib mergesort time: %10.2f us per iteration\n", total_time / RUNS);
#endif
srand48(SEED);
total_time = 0.0;
for (i = 0; i < RUNS; i++) {
fill(arr, SIZE);
memcpy(dst, arr, sizeof(int64_t) * SIZE);
start_time = utime();
sorter_quick_sort(dst, SIZE);
end_time = utime();
total_time += end_time - start_time;
verify(dst, SIZE);
}
printf("quick sort time: %10.2f us per iteration\n", total_time / RUNS);
srand48(SEED);
total_time = 0.0;
for (i = 0; i < RUNS; i++) {
fill(arr, SIZE);
memcpy(dst, arr, sizeof(int64_t) * SIZE);
start_time = utime();
sorter_selection_sort(dst, SIZE);
end_time = utime();
total_time += end_time - start_time;
verify(dst, SIZE);
}
printf("selection sort time: %10.2f us per iteration\n", total_time / RUNS);
srand48(SEED);
total_time = 0.0;
for (i = 0; i < RUNS; i++) {
fill(arr, SIZE);
memcpy(dst, arr, sizeof(int64_t) * SIZE);
start_time = utime();
sorter_merge_sort(dst, SIZE);
end_time = utime();
total_time += end_time - start_time;
verify(dst, SIZE);
}
printf("merge sort time: %10.2f us per iteration\n", total_time / RUNS);
srand48(SEED);
total_time = 0.0;
for (i = 0; i < RUNS; i++) {
fill(arr, SIZE);
memcpy(dst, arr, sizeof(int64_t) * SIZE);
start_time = utime();
sorter_binary_insertion_sort(dst, SIZE);
end_time = utime();
total_time += end_time - start_time;
verify(dst, SIZE);
}
printf("binary insertion sort time: %10.2f us per iteration\n", total_time / RUNS);
srand48(SEED);
total_time = 0.0;
for (i = 0; i < RUNS; i++) {
fill(arr, SIZE);
memcpy(dst, arr, sizeof(int64_t) * SIZE);
start_time = utime();
sorter_heap_sort(dst, SIZE);
end_time = utime();
total_time += end_time - start_time;
verify(dst, SIZE);
}
printf("heap sort time: %10.2f us per iteration\n", total_time / RUNS);
srand48(SEED);
total_time = 0.0;
for (i = 0; i < RUNS; i++) {
fill(arr, SIZE);
memcpy(dst, arr, sizeof(int64_t) * SIZE);
start_time = utime();
sorter_shell_sort(dst, SIZE);
end_time = utime();
total_time += end_time - start_time;
verify(dst, SIZE);
}
printf("shell sort time: %10.2f us per iteration\n", total_time / RUNS);
srand48(SEED);
total_time = 0.0;
for (i = 0; i < RUNS; i++) {
fill(arr, SIZE);
memcpy(dst, arr, sizeof(int64_t) * SIZE);
start_time = utime();
sorter_tim_sort(dst, SIZE);
end_time = utime();
total_time += end_time - start_time;
verify(dst, SIZE);
}
printf("tim sort time: %10.2f us per iteration\n", total_time / RUNS);
srand48(SEED);
total_time = 0.0;
for (i = 0; i < RUNS; i++) {
fill(arr, SIZE);
memcpy(dst, arr, sizeof(int64_t) * SIZE);
start_time = utime();
sorter_merge_sort_in_place(dst, SIZE);
end_time = utime();
total_time += end_time - start_time;
verify(dst, SIZE);
}
printf("in-place merge sort time: %10.2f us per iteration\n", total_time / RUNS);
srand48(SEED);
total_time = 0.0;
for (i = 0; i < RUNS; i++) {
fill(arr, SIZE);
memcpy(dst, arr, sizeof(int64_t) * SIZE);
start_time = utime();
sorter_grail_sort(dst, SIZE);
end_time = utime();
total_time += end_time - start_time;
verify(dst, SIZE);
}
printf("grail sort time: %10.2f us per iteration\n", total_time / RUNS);
srand48(SEED);
total_time = 0.0;
for (i = 0; i < RUNS; i++) {
fill(arr, SIZE);
memcpy(dst, arr, sizeof(int64_t) * SIZE);
start_time = utime();
sorter_sqrt_sort(dst, SIZE);
end_time = utime();
total_time += end_time - start_time;
verify(dst, SIZE);
}
printf("sqrt sort time: %10.2f us per iteration\n", total_time / RUNS);
srand48(SEED);
total_time = 0.0;
for (i = 0; i < RUNS; i++) {
fill(arr, SIZE);
memcpy(dst, arr, sizeof(int64_t) * SIZE);
start_time = utime();
sorter_rec_stable_sort(dst, SIZE);
end_time = utime();
total_time += end_time - start_time;
verify(dst, SIZE);
}
printf("rec stable sort sort time: %10.2f us per iteration\n", total_time / RUNS);
srand48(SEED);
total_time = 0.0;
for (i = 0; i < RUNS; i++) {
fill(arr, SIZE);
memcpy(dst, arr, sizeof(int64_t) * SIZE);
start_time = utime();
sorter_grail_sort_dyn_buffer(dst, SIZE);
end_time = utime();
total_time += end_time - start_time;
verify(dst, SIZE);
}
printf("grail sort dyn buffer sort time: %10.2f us per iteration\n", total_time / RUNS);
}
int main(void) {
run_tests();
return 0;
}