-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_benchmark.cpp
More file actions
103 lines (79 loc) · 3.17 KB
/
memory_benchmark.cpp
File metadata and controls
103 lines (79 loc) · 3.17 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
#include <chrono>
#include <vector>
#include <thread>
#include <iostream>
#include <allocator.h>
using namespace WW;
constexpr size_type THREAD = 4; // 线程数
constexpr size_type ROUND = 1000; // 轮数
constexpr size_type SIZE = 128; // 单次操作内存大小
constexpr size_type TIMES = 1000; // 单次测试操作次数
/**
* @brief 测试结构
*/
class TestCase
{
public:
char padding[SIZE];
};
using high_resolution_clock = std::chrono::high_resolution_clock;
using time_point = std::chrono::high_resolution_clock::time_point;
using duration = std::chrono::duration<double, std::milli>;
int main()
{
printf("================================== MEMORY BENCHMARK =======================================\n");
printf("=== MALLOC TEST ===========================================================================\n");
// malloc测试
std::vector<std::thread> malloc_threads;
malloc_threads.reserve(THREAD);
time_point malloc_start = high_resolution_clock::now();
for (size_type i = 0; i < THREAD; i++) {
malloc_threads.emplace_back([]() {
for (size_type j = 0; j < ROUND; j++) {
std::vector<void *> ptrs;
ptrs.reserve(TIMES);
for (size_type k = 0; k < TIMES; k++) {
void * ptr = malloc(SIZE);
ptrs.emplace_back(ptr);
}
for (void * ptr : ptrs) {
free(ptr);
}
}
});
}
for (auto & thread : malloc_threads) {
thread.join();
}
time_point malloc_end = high_resolution_clock::now();
duration malloc_duration = malloc_end - malloc_start;
printf("%zu threads operated %zu rounds with %zu times each malloc and free, cost %.2f ms\n", THREAD, ROUND, TIMES, malloc_duration.count());
printf("=== MEMORY POOL TEST ======================================================================\n");
// memory-pool测试
std::vector<std::thread> pool_threads;
pool_threads.reserve(THREAD);
time_point pool_start = high_resolution_clock::now();
for (size_type i = 0; i < THREAD; ++i) {
pool_threads.emplace_back([i]() {
allocator<TestCase> alloc;
for (size_type j = 0; j < ROUND; ++j) {
std::vector<TestCase *> ptrs;
ptrs.reserve(TIMES);
for (size_type k = 0; k < TIMES; ++k) {
TestCase * ptr = alloc.allocate(1);
ptrs.emplace_back(ptr);
}
for (size_type k = 0; k < TIMES; ++k) {
alloc.deallocate(ptrs[k], 1);
}
}
});
}
for (auto & thread : pool_threads) {
thread.join();
}
time_point pool_end = high_resolution_clock::now();
duration pool_duration = pool_end - pool_start;
printf("%zu threads operated %zu rounds with %zu times each allocate and deallocate, cost %.2f ms\n", THREAD, ROUND, TIMES, pool_duration.count());
printf("===========================================================================================\n");
}