-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.c
410 lines (363 loc) · 10.9 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*! \file demo.c
\brief A benchmark demo driver for medians_1D and a couple of other methods.
Comparison of qsort-based and optimized median search methods for a
1-D data array. The data type is flexible.
Original code by Nicolas Devillard <ndevilla@free.fr> August 1998.
Modified by Stephen Arnold <stephen.arnold@acm.org> August 2005.
This code is now licensed under the LGPLv3. See the LICENSE file
for details.
A note about this benchmarking method:
The reported times indicate the actual time spent on CPU, they are
quite dependent on CPU load. However, the test is quite fast and it
is reasonable to assume a constant machine load throughout the test.
*/
static const char rcsid[] =
"$Id";
#include "medians_1D.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include <float.h>
//! Number of elements in the target array
#define BIG_NUM (1024*1024)
//! Random test data; generated values are in [0...MAX_ARRAY_VALUE-1]
#define MAX_ARRAY_VALUE 1024
//! Number of search methods tested
#define N_METHODS 5
//! Macro to determine an integer's oddness
#define odd(x) ((x)&1)
// Additional required function prototypes
void bench(int, size_t);
int compare(const void *, const void*);
void pixel_qsort(pixelvalue *, int);
pixelvalue median_AHU(pixelvalue *, int);
pixelvalue select_k(int, pixelvalue *, int);
void bench(int verbose, size_t array_size)
{
int i;
int mednum;
pixelvalue med[N_METHODS];
clock_t chrono;
double elapsed;
pixelvalue * array_init,
* array;
//! Random number seed
/*! Initialize random generator with PID.
This is the only Unix-ish thing; can be replaced with an
alternate scheme.
*/
srand48(getpid());
if (verbose) {
printf("generating element values...\n");
fflush(stdout);
}
if (array_size<1) array_size = BIG_NUM;
if (verbose) {
printf("array size: %ld\n", (long)array_size);
} else {
printf("%ld\t", (long)array_size);
}
array_init = malloc(array_size * sizeof(pixelvalue));
array = malloc(array_size * sizeof(pixelvalue));
if (array_init==NULL || array==NULL) {
printf("memory allocation failure: aborting\n");
return ;
}
for (i=0 ; i<array_size; i++) {
array_init[i] = (pixelvalue)(lrand48() % MAX_ARRAY_VALUE);
}
mednum = 0;
//! benchmark the quickselect sort
memcpy(array, array_init, array_size * sizeof(pixelvalue));
if (verbose) {
printf("quick select :\t");
fflush(stdout);
}
chrono = clock();
med[mednum] = quick_select(array, array_size);
elapsed = (double)(clock() - chrono) / (double)CLOCKS_PER_SEC;
if (verbose) {
printf("%5.3f sec\t", elapsed);
fflush(stdout);
printf("med %g\n", (double)med[mednum]);
fflush(stdout);
} else {
printf("%5.3f\t", elapsed);
fflush(stdout);
}
mednum++;
//! benchmark wirth function
memcpy(array, array_init, array_size * sizeof(pixelvalue));
if (verbose) {
printf("Wirth median :\t");
fflush(stdout);
}
chrono = clock();
med[mednum] = wirth(array, array_size);
elapsed = (double)(clock() - chrono) / (double)CLOCKS_PER_SEC;
if (verbose) {
printf("%5.3f sec\t", elapsed);
fflush(stdout);
printf("med %g\n", (double)med[mednum]);
fflush(stdout);
} else {
printf("%5.3f\t", elapsed);
fflush(stdout);
}
mednum++;
//! benchmark AHU sort
memcpy(array, array_init, array_size * sizeof(pixelvalue));
if (verbose) {
printf("AHU median :\t");
fflush(stdout);
}
chrono = clock();
med[mednum] = median_AHU(array, array_size);
elapsed = (double)(clock() - chrono) / (double)CLOCKS_PER_SEC;
if (verbose) {
printf("%5.3f sec\t", elapsed);
fflush(stdout);
printf("med %g\n", (double)med[mednum]);
fflush(stdout);
} else {
printf("%5.3f\t", elapsed);
fflush(stdout);
}
mednum++;
//! benchmark torben's method
memcpy(array, array_init, array_size * sizeof(pixelvalue));
if (verbose) {
printf("torben :\t");
fflush(stdout);
}
chrono = clock();
med[mednum] = torben(array, array_size);
elapsed = (double)(clock() - chrono) / (double)CLOCKS_PER_SEC;
if (verbose) {
printf("%5.3f sec\t", elapsed);
fflush(stdout);
printf("med %g\n", (double)med[mednum]);
fflush(stdout);
} else {
printf("%5.3f\t", elapsed);
fflush(stdout);
}
mednum++;
//! benchmark the eclipse fast pixel sort
memcpy(array, array_init, array_size * sizeof(pixelvalue));
if (verbose) {
printf("fast pixel sort :\t");
fflush(stdout);
}
chrono = clock();
pixel_qsort(array, array_size);
elapsed = (double)(clock() - chrono) / (double)CLOCKS_PER_SEC;
if (odd(array_size)) {
med[mednum] = array[array_size/2];
} else {
med[mednum] = array[(array_size/2) -1];
}
if (verbose) {
printf("%5.3f sec\t", elapsed);
fflush(stdout);
printf("med %g\n", (double)med[mednum]);
fflush(stdout);
} else {
printf("%5.3f\t", elapsed);
fflush(stdout);
}
mednum++;
free(array);
free(array_init);
for (i=1 ; i<N_METHODS ; i++) {
if (fabs(med[i-1] - med[i]) > 10 * FLT_EPSILON) {
printf("diverging median values!\n");
fflush(stdout);
}
}
printf("\n");
fflush(stdout);
return;
}
//! This function is only useful to the qsort() routine
int compare(const void *f1, const void *f2)
{ return ( *(pixelvalue*)f1 > *(pixelvalue*)f2) ? 1 : -1 ; }
/*! \fn void pixel_qsort(pixelvalue *, int)
\brief Old and supposedly optimized quicksort algorithm
Function : pixel_qsort()
- In : pixel array, size of the array
- Out : void
- Job : sort out the array of pixels
- Note : optimized implementation, unreadable.
*/
#define PIX_SWAP(a,b) { pixelvalue temp=(a);(a)=(b);(b)=temp; }
#define PIX_STACK_SIZE 50
void pixel_qsort(pixelvalue *pix_arr, int npix)
{
int i,
ir,
j,
k,
l;
int * i_stack;
int j_stack;
pixelvalue a;
ir = npix;
l = 1;
j_stack = 0;
i_stack = malloc(PIX_STACK_SIZE * sizeof(pixelvalue));
for (;;) {
if (ir-l < 7) {
for (j=l+1 ; j<=ir ; j++) {
a = pix_arr[j-1];
for (i=j-1 ; i>=1 ; i--) {
if (pix_arr[i-1] <= a) break;
pix_arr[i] = pix_arr[i-1];
}
pix_arr[i] = a;
}
if (j_stack == 0) break;
ir = i_stack[j_stack-- -1];
l = i_stack[j_stack-- -1];
} else {
k = (l+ir) >> 1;
PIX_SWAP(pix_arr[k-1], pix_arr[l])
if (pix_arr[l] > pix_arr[ir-1]) {
PIX_SWAP(pix_arr[l], pix_arr[ir-1])
}
if (pix_arr[l-1] > pix_arr[ir-1]) {
PIX_SWAP(pix_arr[l-1], pix_arr[ir-1])
}
if (pix_arr[l] > pix_arr[l-1]) {
PIX_SWAP(pix_arr[l], pix_arr[l-1])
}
i = l+1;
j = ir;
a = pix_arr[l-1];
for (;;) {
do i++; while (pix_arr[i-1] < a);
do j--; while (pix_arr[j-1] > a);
if (j < i) break;
PIX_SWAP(pix_arr[i-1], pix_arr[j-1]);
}
pix_arr[l-1] = pix_arr[j-1];
pix_arr[j-1] = a;
j_stack += 2;
if (j_stack > PIX_STACK_SIZE) {
printf("stack too small in pixel_qsort: aborting");
exit(-2001) ;
}
if (ir-i+1 >= j-l) {
i_stack[j_stack-1] = ir;
i_stack[j_stack-2] = i;
ir = j-1;
} else {
i_stack[j_stack-1] = j-1;
i_stack[j_stack-2] = l;
l = i;
}
}
}
free(i_stack);
}
#undef PIX_STACK_SIZE
#undef PIX_SWAP
/*! \fn pixelvalue select_k(int, pixelvalue *, int)
\brief Called by median_AHU()
Function : select_k()
- In : element to search for, list of pixelvalues, # of values
- Out : pixelvalue
- Job : find out the kth smallest value of the list
- Note : recursively called by median_AHU()
*/
pixelvalue select_k(int k, pixelvalue * list, int n)
{
int n1 = 0,
n2 = 0,
n3 = 0;
pixelvalue * S;
int i, j;
pixelvalue p;
if (n==1) return list[0];
p = list[(n>>1)];
for (i=0 ; i<n ; i++) {
if (list[i]<p) {
n1++;
} else if (fabs(list[i] - p) < 10 * FLT_EPSILON) {
n2++;
} else {
n3++;
}
}
if (n1>=k) {
S = malloc(n1*sizeof(pixelvalue));
j = 0;
for (i=0 ; i<n ; i++) {
if (list[i]<p) S[j++] = list[i];
}
p = select_k(k, S, n1);
free(S);
} else {
if ((n1+n2)<k) {
S = malloc(n3 * sizeof(pixelvalue));
j = 0;
for (i=0 ; i<n ; i++) {
if (list[i]>p) S[j++] = list[i];
}
p = select_k(k-n1-n2, S, n3);
free(S);
}
}
return p;
}
//! This function uses select_k to find the median
pixelvalue median_AHU(pixelvalue * list, int n)
{
if (odd(n)) {
return select_k((n/2)+1, list, n);
} else {
return select_k((n/2), list, n);
}
}
//! Main driver demo for median search routines
int main(int argc, char * argv[])
{
int i;
int from, to, step;
int count;
if (argc<2) {
printf("usage:\n");
printf("%s <n>\n", argv[0]);
printf("\tif n=1 the output is verbose for one attempt\n");
printf("\tif n>1 the output reads:\n");
printf("\t# of elements | method1 | method2 | ...\n");
printf("\n");
printf("%s <from> <to> <step>\n", argv[0]);
printf("\twill loop over the number of elements in input\n");
printf("\n");
exit(EXIT_FAILURE);
}
if (argc==2) {
count = atoi(argv[1]);
if (count==1) {
bench(1, BIG_NUM);
} else {
printf("Size\tQS\tWirth\tAHU\tTorben\tpixel sort\n");
for (i=0 ; i<atoi(argv[1]) ; i++) {
bench(0, BIG_NUM);
}
}
} else if (argc==4) {
from = atoi(argv[1]);
to = atoi(argv[2]);
step = atoi(argv[3]);
for (count=from ; count<=to ; count+=step) {
bench(0, count);
}
return EXIT_SUCCESS;
}
return EXIT_SUCCESS;
}