-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathmonobound_bsearch.c
More file actions
164 lines (117 loc) · 3.28 KB
/
monobound_bsearch.c
File metadata and controls
164 lines (117 loc) · 3.28 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
// Binary Search v1.7 - Igor van den Hoven ivdhoven@gmail.com
// Compile using: gcc -O3 monobound_bsearch.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
int checks;
// Avoid inlining to guarantee the benchmark is fair.
__attribute__ ((noinline)) int cmp_int(const void * a, const void * b)
{
int fa = *(int *)a;
int fb = *(int *)b;
checks++;
return fa - fb;
}
// Monobound binary search using gcc's bsearch() interface. Since the
// comparison is slow there's no deferred detection of equality.
void *monobound_bsearch(const void *key, const void *base0, size_t nmemb, size_t size, int (*compar)(const void *, const void *))
{
char *piv, *base = (char *) base0;
int cmp;
while (nmemb)
{
nmemb /= 2;
piv = base + nmemb * size;
cmp = (*compar)(key, piv);
if (cmp == 0)
return (void *) piv;
if (cmp > 0)
base = piv + size;
}
return NULL;
}
// benchmark
long long utime()
{
struct timeval now_time;
gettimeofday(&now_time, NULL);
return now_time.tv_sec * 1000000LL + now_time.tv_usec;
}
int *o_array, *r_array;
int density, max, loop, top, rnd, runs;
long long start, end, best;
void execute(void *(*algo_func)(const void *, const void *, size_t, size_t, int (*cmp)(const void *, const void *)), const char * algo_name)
{
unsigned int cnt, hit, miss;
srand(rnd);
best = 0;
for (int run = runs ; run ; --run)
{
hit = 0;
miss = 0;
checks = 0;
start = utime();
for (cnt = 0 ; cnt < loop ; cnt++)
{
if (algo_func(r_array + cnt, o_array, max, sizeof(int), cmp_int) != NULL)
{
hit++;
}
else
{
miss++;
}
}
end = utime();
if (best == 0 || end - start < best)
{
best = end - start;
}
}
printf("| %30s | %10d | %10d | %10d | %10d | %10f |\n", algo_name, max, hit, miss, checks, best / 1000000.0);
}
#define run(algo) execute(&algo, #algo)
int main(int argc, char **argv)
{
int cnt, val;
max = 100000;
loop = 10000;
density = 10; // max * density should stay under 2 billion
runs = 1000;
rnd = time(NULL);
if (argc > 1)
max = atoi(argv[1]);
if (argc > 2)
runs = atoi(argv[2]);
if (argc > 3)
loop = atoi(argv[3]);
if (argc > 4)
rnd = atoi(argv[4]);
o_array = (int *) malloc(max * sizeof(int));
r_array = (int *) malloc(loop * sizeof(int));
if ((long long) max * (long long) density > 2000000000)
{
density = 2;
}
for (cnt = 0, val = 0 ; cnt < max ; cnt++)
{
o_array[cnt] = (val += rand() % (density * 2));
}
top = o_array[max - 1] + density;
srand(rnd);
for (cnt = 0 ; cnt < loop ; cnt++)
{
r_array[cnt] = rand() % top;
}
printf("Benchmark: array size: %d, runs: %d, repetitions: %d, seed: %d, density: %d\n\n", max, runs, loop, rnd, density);
printf("Even distribution with %d 32 bit integers, random access\n\n", max);
printf("| %30s | %10s | %10s | %10s | %10s | %10s |\n", "Name", "Items", "Hits", "Misses", "Checks", "Time");
printf("| %30s | %10s | %10s | %10s | %10s | %10s |\n", "----------", "----------", "----------", "----------", "----------", "----------");
run(monobound_bsearch);
run(bsearch);
free(o_array);
free(r_array);
return 0;
}