forked from dcshi/ncx_mempool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.c
79 lines (59 loc) · 1.25 KB
/
bench.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
#include "ncx_slab.h"
#include <sys/time.h>
uint64_t usTime()
{
struct timeval tv;
uint64_t usec;
gettimeofday(&tv, NULL);
usec = ((uint64_t)tv.tv_sec)*1000000LL;
usec += tv.tv_usec;
return usec;
}
int main(int argc, char **argv)
{
ncx_slab_pool_t *sp;
size_t pool_size;
u_char *space;
pool_size = 4096000; //4M
space = (u_char *)malloc(pool_size);
sp = (ncx_slab_pool_t*) space;
sp->addr = space;
sp->min_shift = 3;
sp->end = space + pool_size;
ncx_slab_init(sp);
char *p;
uint64_t us_begin;
uint64_t us_end;
size_t size[] = { 30, 120, 256, 500, 1000, 2000, 3000, 5000};
printf("size\tncx\tmalloc\tpct\n");
int i, j;
uint64_t t1, t2;
for (j = 0; j < sizeof(size)/sizeof(size_t); j++)
{
size_t s = size[j];
printf("%d\t", s);
//test for ncx_pool
us_begin = usTime();
for(i = 0; i < 1000000; i++)
{
p = ncx_slab_alloc(sp, s);
ncx_slab_free(sp, p);
}
us_end = usTime();
t1 = (us_end - us_begin);
printf("%llu \t", t1/1000);
// test for malloc
us_begin = usTime();
for(i = 0; i < 1000000; i++)
{
p = (char*)malloc(s);
free(p);
}
us_end = usTime();
t2 = (us_end - us_begin);
printf("%llu\t", t2/1000);
printf("%.2f\n", (double)t1 / (double)t2);
}
free(space);
return 0;
}