forked from yyz845935161/ConcurrentMemoryPool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBenchmark.cpp
152 lines (138 loc) · 4.79 KB
/
Benchmark.cpp
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
#include "include/ConcurrentAlloc.h"
#include <atomic>
void *AllocBenchmarkMalloc(void *arg)
{
size_t *args = (size_t *)arg;
size_t ntimes = args[0];
size_t rounds = args[1];
size_t *malloc_costtime = (size_t *)*(args + 2);
size_t *free_costtime = (size_t *)*(args + 3);
std::vector<void *> v;
v.reserve(ntimes);
for (size_t j = 0; j < rounds; ++j)
{
size_t begin1 = clock();
for (size_t i = 0; i < ntimes; i++)
{
// v.push_back(malloc(16));
v.push_back(malloc((16 + i) % 4096 + 1));
}
size_t end1 = clock();
size_t begin2 = clock();
for (size_t i = 0; i < ntimes; i++)
{
free(v[i]);
}
size_t end2 = clock();
v.clear();
*malloc_costtime += end1 - begin1;
*free_costtime += end2 - begin2;
}
return nullptr;
}
void *AllocBenchmarkConcurrentMalloc(void *arg)
{
size_t *args = (size_t *)arg;
size_t ntimes = args[0];
size_t rounds = args[1];
size_t *malloc_costtime = (size_t *)*(args + 2);
size_t *free_costtime = (size_t *)*(args + 3);
std::vector<void *> v;
v.reserve(ntimes);
for (size_t j = 0; j < rounds; ++j)
{
size_t begin1 = clock();
for (size_t i = 0; i < ntimes; i++)
{
// v.push_back(ConcurrentAlloc(16));
v.push_back(ConcurrentAlloc((16 + i) % 4096 + 1));
}
size_t end1 = clock();
size_t begin2 = clock();
for (size_t i = 0; i < ntimes; i++)
{
// ConcurrentFree(v[i],16);
ConcurrentFree(v[i],(16 + i) % 4096 + 1);
}
size_t end2 = clock();
v.clear();
*malloc_costtime += (end1 - begin1);
*free_costtime += (end2 - begin2);
}
return nullptr;
}
//malloc效率
void BenchmarkMalloc(size_t ntimes, size_t nworks, size_t rounds)
{
// ntimes 一轮申请和释放的轮次
// nworks 伦次
// nworks 线程次
std::vector<pthread_t> vthread(nworks);
std::atomic<size_t> malloc_costtime = {0};
std::atomic<size_t> free_costtime = {0};
// size_t malloc_costtime=0;
// size_t free_costtime=0;
for (size_t k = 0; k < nworks; ++k)
{
size_t args[4];
args[0] = ntimes;
args[1] = rounds;
args[2] = (size_t)(&malloc_costtime);
args[3] = (size_t)(&free_costtime);
pthread_create(&vthread[k], NULL, AllocBenchmarkMalloc, (void *)args);
}
for (auto &t : vthread)
{
pthread_join(t, NULL);
}
double _malloc_costtime = (double)malloc_costtime/ CLOCKS_PER_SEC * 1000;
double _free_costtime = (double)free_costtime/ CLOCKS_PER_SEC * 1000;
printf("malloc 下性能:\n");
printf("%zu个线程并发执行%zu轮次,每轮次malloc %zu次: 花费:%.f ms\n",
nworks, rounds, ntimes, _malloc_costtime);
printf("%zu个线程并发执行%zu轮次,每轮次free %zu次: 花费:%.f ms\n",
nworks, rounds, ntimes, _free_costtime);
printf("%zu个线程并发malloc&free %zu次,总计花费:%.f ms\n",
nworks, nworks * rounds * ntimes, (double)(_malloc_costtime + _free_costtime));
}
// 单轮次申请释放次数 线程数 轮次
void BenchmarkConcurrentMalloc(size_t ntimes, size_t nworks, size_t rounds)
{
std::vector<pthread_t> vthread(nworks);
std::atomic<size_t> malloc_costtime = {0};
std::atomic<size_t> free_costtime = {0};
// size_t malloc_costtime=0;
// size_t free_costtime=0;
for (size_t k = 0; k < nworks; ++k)
{
size_t args[4];
args[0] = ntimes;
args[1] = rounds;
args[2] = (size_t)(&malloc_costtime);
args[3] = (size_t)(&free_costtime);
pthread_create(&vthread[k], NULL, AllocBenchmarkConcurrentMalloc, (void *)args);
}
for (auto &t : vthread)
{
pthread_join(t, NULL);
}
double _malloc_costtime = (double)malloc_costtime/ CLOCKS_PER_SEC * 1000;
double _free_costtime = (double)free_costtime/ CLOCKS_PER_SEC * 1000;
printf("ConcurrentMemoryPool 下性能:\n");
printf("%zu个线程并发执行%zu轮次,每轮次malloc %zu次: 花费:%.f ms\n",
nworks, rounds, ntimes, _malloc_costtime);
printf("%zu个线程并发执行%zu轮次,每轮次free %zu次: 花费:%.f ms\n",
nworks, rounds, ntimes, _free_costtime);
printf("%zu个线程并发malloc&free %zu次,总计花费:%.f ms\n",
nworks, nworks * rounds * ntimes, (double)(_malloc_costtime + _free_costtime));
}
int main()
{
cout << "==========================================================" << endl;
BenchmarkMalloc(10000, 5, 100);
cout << endl
<< endl;
BenchmarkConcurrentMalloc(10000, 5, 100);
cout << "==========================================================" << endl;
return 0;
}