-
Notifications
You must be signed in to change notification settings - Fork 0
/
measure.c
executable file
·165 lines (137 loc) · 3.57 KB
/
measure.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
#include "hashtable.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#ifdef __APPLE__
#include <mach/mach_time.h>
#elif _WIN32
#include <windows.h>
#elif __linux__
#include <time.h>
#endif
int freecnt = 0;
int insertcnt;
hashtable_hash_t mt_hash(void *data) {
return (hashtable_hash_t)((intptr_t)data);
}
int mt_compare(void *data, void *key) {
return (data == key);
}
void mt_free(void *key) {
freecnt++;
}
void timeThis(void) {
#ifdef __APPLE__
static uint64_t stime=0, etime;
mach_timebase_info_data_t info;
uint64_t diff, titm;
if (stime) {
etime = mach_absolute_time();
mach_timebase_info(&info);
diff = (etime - stime) * info.numer / info.denom;
titm = (diff * 100) / insertcnt;
printf("Time spent: %lld (per item %lld.%lld) ns\n", diff, titm / 100, titm % 100);
stime = etime = 0;
} else {
stime = mach_absolute_time();
}
#elif _WIN32
static LONGLONG stime=0, etime;
LONGLONG freq;
uint64_t tmp1, tmp2, diff, titm;
if (stime) {
QueryPerformanceCounter((LARGE_INTEGER*)&etime);
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
diff = etime - stime;
tmp1 = (diff * 1000000) / freq;
tmp2 = ((diff * 1000000) % freq) * 1000 / freq;
diff = tmp1 * 1000 + tmp2;
titm = (diff * 100) / insertcnt;
printf("Time spent: %lld (per item %lld.%lld) ns\n", diff, titm / 100, titm % 100);
stime = etime = 0;
} else {
QueryPerformanceCounter((LARGE_INTEGER*)&stime);
}
#elif __linux__
static struct timespec stime, etime;
static int started = 0;
uint64_t t0, t1, diff, titm;
if (started) {
clock_gettime(CLOCK_MONOTONIC_RAW, &etime);
t0 = stime.tv_nsec + stime.tv_sec * 1000000000;
t1 = etime.tv_nsec + etime.tv_sec * 1000000000;
diff = t1 - t0;
titm = (diff * 100) / insertcnt;
printf("Time spent: %lld (per item %lld.%lld) ns\n", diff, titm / 100, titm % 100);
started = 0;
} else {
started = 1;
clock_gettime(CLOCK_MONOTONIC_RAW, &stime);
}
#endif
}
int main(int argc, char *argv[]) {
hashtable_t *ht;
int cbuckets, i;
unsigned rand1, rand2;
hashtable_enum_t *s;
printf("Test size? ");
scanf("%d", &insertcnt);
printf("Bucket count? ");
scanf("%d", &cbuckets);
ht = hashtable_make(cbuckets, mt_compare, mt_hash, mt_free, NULL);
printf("Control... ");
timeThis();
timeThis();
printf("Insertion... ");
srand(insertcnt);
timeThis();
for (i=0; i<insertcnt; i++) {
hashtable_insert(ht, (void*)((intptr_t)rand()+1), (void*)1);
}
timeThis();
printf("Retrieval... ");
srand(insertcnt);
timeThis();
for (i=0; i<insertcnt; i++) {
hashtable_search(ht, (void*)((intptr_t)rand()+1));
}
timeThis();
printf("Enumeration... ");
srand(insertcnt);
timeThis();
s = hashtable_enumerate(NULL, ht, (void**)&rand1, NULL);
while (s) {
s = hashtable_enumerate(s, ht, (void**)&rand1, NULL);
}
timeThis();
printf("Removal... ");
srand(insertcnt);
timeThis();
for (i=0; i<insertcnt; i++) {
hashtable_remove(ht, (void*)((intptr_t)rand()+1));
}
timeThis();
if (insertcnt != freecnt) {
printf("Something's wrong. %d %d\n", insertcnt, freecnt);
}
printf("Insertion 2... ");
srand(insertcnt);
timeThis();
for (i=0; i<insertcnt; i++) {
hashtable_insert(ht, (void*)((intptr_t)rand()+1), (void*)1);
}
timeThis();
freecnt = 0;
printf("Free... ");
timeThis();
hashtable_free(ht);
timeThis();
if (insertcnt != freecnt) {
printf("Something's wrong. %d %d\n", insertcnt, freecnt);
}
printf("all ok\n");
return 0;
}