This repository has been archived by the owner on Aug 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rb_test.c
126 lines (107 loc) · 2.38 KB
/
rb_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
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
#include <stdio.h>
#include "time.h"
#include "rbtree.h"
struct _obj_t {
int key;
struct rb_node node;
char data[32];
};
typedef struct _obj_t obj_t;
obj_t*
my_search(struct rb_root *root, int key)
{
struct rb_node *node = root->rb_node;
while (node) {
obj_t *data = container_of(node, struct _obj_t, node);
int result;
result = key - data->key;
if (result < 0)
node = node->rb_left;
else if (result > 0)
node = node->rb_right;
else
return data;
}
return NULL;
}
int
rb_insert_obj(struct rb_root *root, obj_t *data)
{
obj_t *this;
struct rb_node **p = &(root->rb_node), *parent = NULL;
while (*p)
{
parent = *p;
this = container_of(*p, struct _obj_t, node);
int result = data->key - this->key;
parent = *p;
if (result < 0)
p = &(*p)->rb_left;
else if (result > 0)
p = &(*p)->rb_right;
else
return -1;
}
rb_link_node(&data->node, parent, p);
rb_insert_color(&data->node, root);
return 0;
}
#define MAX_SET 100000
#define SEARCH_SET 5
_time_t duration[MAX_SET];
int search_set[SEARCH_SET] = {432, 51236, 234124, 432342323, 4232};
int main (void)
{
struct rb_root mytree = RB_ROOT;
obj_t *obj = NULL;
int i, ret;
_time_t t1, t2,t3;
for (i = 0; i< MAX_SET; i++) {
obj_t *o1 = malloc(sizeof(obj_t));
o1->key = i;
t1 = rdtsc();
ret = rb_insert_obj(&mytree, o1);
t2 = rdtsc();
duration[i] = tick_diff_usecs(t1, t2);
if (ret != 0)
printf("failed to insert %d in tree\n", i);
}
for (i = 0; i < SEARCH_SET; i++) {
t1 = rdtsc();
obj = my_search(&mytree, search_set[i]);
t2 = rdtsc();
t3 = tick_diff_usecs(t1, t2);
if (obj) {
printf("found obj %d in %llu usecs\n", obj->key, t3);
} else
printf("obj not found in %llu usecs\n", t3);
}
printf("sleeping for 10 secs\n");
sleep(10);
for (i = 0; i< MAX_SET; i++) {
if (duration[i] > 0)
printf("obj %d took %llu to insert\n", i, duration[i]);
}
for (i = 0; i< MAX_SET; i++) {
if (duration[i] > 5)
printf("obj %d took %llu to insert\n", i, duration[i]);
}
printf("sleeping for 10 secs\n");
sleep(10);
for (i = 0; i< MAX_SET; i++) {
t1 = rdtsc();
obj = my_search(&mytree, i);
if (obj) {
rb_erase(&obj->node, &mytree);
free(obj);
}
t2 = rdtsc();
t3 = tick_diff_usecs(t1, t2);
duration[i] = tick_diff_usecs(t1, t2);
}
for (i = 0; i< MAX_SET; i++) {
if (duration[i] > 5)
printf("obj %d took %llu to find/delete\n", i, duration[i]);
}
return 0;
}