-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_str.c
More file actions
184 lines (156 loc) · 4.5 KB
/
Copy pathtest_str.c
File metadata and controls
184 lines (156 loc) · 4.5 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2023 luo-zeqi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include "mempool.h"
#include "llist.h"
#define ARR_LEN(arr, type) (sizeof(arr) / sizeof(type))
#define USE_XMEMPOOL
#define XMEM_BLOCK_SIZE 16
#ifdef USE_XMEMPOOL
xmempool_t mp;
#endif
// The data generate by Chat-GPT.
const char *test_data[] = {
"Hello World!",
"Live and let live.",
"Time is money.",
"Practice makes perfect.",
"All is fair in love and war.",
"Actions speak louder than words.",
"Beauty is in the eye of the beholder.",
"Knowledge is power.",
"Where there's smoke, there's fire.",
"You can't have your cake and eat it too.",
"It takes two to tango.",
"Every cloud has a silver lining.",
"The early bird catches the worm.",
"Two heads are better than one.",
"Look before you leap.",
"A picture is worth a thousand words.",
"Rome wasn't built in a day.",
"Practice what you preach.",
"When in Rome, do as the Romans do.",
"Better late than never.",
"Don't count your chickens before they hatch.",
"When the going gets tough, the tough get going.",
"All that glitters is not gold.",
"The grass is always greener on the other side.",
"No pain, no gain.",
"Curiosity killed the cat.",
"Haste makes waste.",
"Don't put all your eggs in one basket.",
"Necessity is the mother of invention.",
"What goes up must come down.",
"A bird in the hand is worth two in the bush.",
"The pen is mightier than the sword.",
"Actions speak louder than words.",
"Love conquers all.",
"Time heals all wounds.",
"Beauty is only skin deep.",
"A watched pot never boils.",
"Better safe than sorry.",
"Good things come to those who wait.",
"Laughter is the best medicine.",
"Where there's a will, there's a way.",
};
int64_t order_alloc() {
int i;
struct timeval t1, t2;
struct xstring {
char *ptr;
} strs[ARR_LEN(test_data, char *)];
gettimeofday(&t1, NULL);
for (i = 0; i < ARR_LEN(test_data, char *); i++) {
#ifdef USE_XMEMPOOL
strs[i].ptr = xmempool_alloc(&mp, (int) strlen(test_data[i]));
#else
strs[i].ptr = malloc(strlen(test_data[i]));
#endif
assert(strs[i].ptr);
memcpy(strs[i].ptr, test_data[i], strlen(test_data[i]));
}
for (i = 0; i < ARR_LEN(test_data, char *); i++) {
#ifdef USE_XMEMPOOL
xmempool_free(&mp, strs[i].ptr);
#else
free(strs[i].ptr);
#endif
}
gettimeofday(&t2, NULL);
return (t2.tv_sec-t1.tv_sec) * 1000000 + (t2.tv_usec - t1.tv_usec);
}
typedef struct xstr {
llnode_t node;
} xstr_t;
static long count = 0;
static xstr_t head, *curr, *tmp;
int64_t random_alloc() {
int i, j;
int size;
long datanum, allocflag, idx;
struct timeval t1, t2;
gettimeofday(&t1, NULL);
curr = &head;
datanum = ARR_LEN(test_data, char *);
for (i = 0; i < datanum * 2; i++) {
allocflag = rand() % 2;
if (allocflag && count < 1000) {
idx = rand() % datanum;
size = (int) (strlen(test_data[idx]) + sizeof(xstr_t));
#ifdef USE_XMEMPOOL
tmp = (xstr_t *) xmempool_alloc(&mp, size);
#else
tmp = (xstr_t *) malloc(size);
#endif
llist_add(&head.node, &tmp->node);
count++;
} else {
if (count == 0) continue;
idx = rand() % count;
curr = &head;
for (j = 0; j <= idx; j++) {
curr = (xstr_t *) (curr->node.next);
}
llist_remove(&curr->node);
#ifdef USE_XMEMPOOL
xmempool_free(&mp, curr);
#else
free(curr);
#endif
count--;
}
}
gettimeofday(&t2, NULL);
return (t2.tv_sec-t1.tv_sec) * 1000000 + (t2.tv_usec - t1.tv_usec);
}
int main() {
int i;
int batch = 100000;
int64_t tuse = 0;
llist_init(&head.node);
//
#ifdef USE_XMEMPOOL
xmempool_init(&mp, 4, 16, 24, 32, 64);
#endif
for (i = 0; i < batch; i++)
tuse += random_alloc();
printf("Average elapsed time: %lld\n", tuse / (int64_t) count);
#ifdef USE_XMEMPOOL
xmempool_destroy(&mp);
#endif
}