-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.c
87 lines (65 loc) · 2.21 KB
/
program.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "hashtable.h"
char *rand_string(uint32_t size)
{
char *str = (char*)malloc(sizeof(char)*size);
// Make sure the set is large enough to facilitate random ordering.
const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
if (size) {
--size;
for (size_t n = 0; n < size; n++) {
uint32_t key = rand() % (uint32_t) (sizeof charset - 1);
str[n] = charset[key];
}
str[size] = '\0';
}
return str;
}
int main(int argc, char** argv) {
if(argc < 2) {
printf("USAGE:\tprogram <count_of_keys>\n");
return 1;
}
const uint32_t SAMPLINGRETRIES = 10;
uint32_t iterations = atoi(argv[1]);
clock_t t;
char* key;
char* value;
/* Initialize the data structure. */
Key** keystore = malloc(sizeof(Key)*iterations);
HashTable* hash = initHashTable();
/* Insert Elements */
uint32_t count = 0;
while(count < iterations) {
key = rand_string(20); value = rand_string(10);
Key* _key = (Key*)malloc(sizeof(Key));
_key->data = key; _key->size = strlen(key);
Value* _value = (Value*)malloc(sizeof(Value));
_value->data = value; _value->size = strlen(value);
insert(hash, _key, _value);
keystore[count] = _key;
count++;
}
/* Fetch Elements */
clock_t timeSum = 0;
for(uint32_t i=0; i<SAMPLINGRETRIES ;i++) {
uint32_t random_index = rand() % (uint32_t)( count );
Key* randomKey = keystore[random_index];
t = clock();
Value* result = find(hash, randomKey);
if(result == NULL) {
printf("Not Found: %s\n", randomKey->data);
exit(1);
}
t = clock() - t;
timeSum += t;
}
/* Result Time */
printf("Average fetch Time: %.2f ns\n", ((double)timeSum*1000000/(CLOCKS_PER_SEC * SAMPLINGRETRIES)));
printf("%d Nodes, %d Collisions:\t%.2f%\n", hash->numberOfNodes, hash->collisions, (float)hash->collisions*100/hash->numberOfNodes);
printf("Final Index Table Size: %d\n", MAX_INDEX_TABLE_SIZE);
return 0;
}