-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.c
78 lines (67 loc) · 1.74 KB
/
test.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
#include "types/table.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#define PROB_INVALID .20
void main(int argc, char **argv) {
if ( argc != 3 ) {
printf("Usage: %s (filename) (number of entries)\n", argv[0]);
exit(1);
}
FILE *fp;
char buf[12];
table t;
mpz_t u;
ulint i, count, invalid, found;
unsigned long long phonenum;
unsigned long long *numlist;
struct timeval starttime, endtime;
ulint starttime_us, endtime_us;
if ( (fp = fopen(argv[1], "r")) == NULL ) {
printf("Could not open file: %s\n", argv[1]);
}
sscanf(argv[2], "%ld", &count);
numlist = (unsigned long long *)calloc( count, sizeof(unsigned long long));
mpz_init(u);
mpz_set_ui(u, 1000 * 1000);
mpz_mul_ui(u, u, 10000);
table_init(&t, count, u);
srand(time(NULL));
i = 0;
invalid = 0;
while ( fgets(buf, 12, fp) != NULL ) {
sscanf(buf, "%lld\n", &phonenum);
numlist[i] = phonenum;
if (rand() <= PROB_INVALID * RAND_MAX) {
numlist[i] = numlist[i] / 4;
invalid++;
}
table_insert(&t, phonenum);
i++;
}
fclose(fp);
printf("Building table...\n");
table_build(&t);
printf("Done inserting %ld numbers (%ld invalids)\n", i, invalid);
i = 0;
found = 0;
invalid = 0;
gettimeofday(&starttime, NULL);
while ( i < count ) {
if ( table_find(&t, numlist[i]) ) {
found++;
} else {
invalid++;
}
i++;
}
gettimeofday(&endtime, NULL);
starttime_us = starttime.tv_sec*1000000 + starttime.tv_usec;
endtime_us = endtime.tv_sec*1000000 + endtime.tv_usec;
printf("Found %ld numbers, rejected %ld\n", found, invalid);
printf("Total find time: %ld us\n", endtime_us-starttime_us);
free(numlist);
table_destroy(&t);
mpz_clear(u);
}