Spaces:
Build error
Build error
File size: 4,131 Bytes
1dd0e3b 4942ce4 1dd0e3b 8ad3629 1dd0e3b | 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 | #include <iostream>
#include <vector>
#include <chrono>
#include "object_pool.h"
#include <cstring>
// 测试结构体
struct TestStruct {
int id;
double value;
char name[32];
TestStruct(int id = 0, double value = 0.0, const char* name = "")
: id(id), value(value) {
strcpy(this->name, name);
}
};
// 性能测试函数
template <typename Func>
double measure_time(Func&& func) {
auto start = std::chrono::high_resolution_clock::now();
func();
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration<double, std::milli>(end - start).count();
}
int main() {
const size_t TEST_SIZE = 10000000; // 1000万个对象
std::cout << "Testing ObjectPool vs std::vector performance..." << std::endl;
std::cout << "Test size: " << TEST_SIZE << " objects" << std::endl;
std::cout << "=========================================" << std::endl;
// 测试 ObjectPool
double pool_time = measure_time([&]() {
hhb::core::ObjectPool<TestStruct> pool;
std::vector<TestStruct*> objects;
objects.reserve(TEST_SIZE);
// 分配对象
for (size_t i = 0; i < TEST_SIZE; ++i) {
TestStruct* obj = pool.allocate();
new (obj) TestStruct(i, i * 1.0, "test");
objects.push_back(obj);
}
// 释放对象
for (TestStruct* obj : objects) {
obj->~TestStruct();
pool.deallocate(obj);
}
});
// 测试 std::vector
double vector_time = measure_time([&]() {
std::vector<TestStruct> objects;
objects.reserve(TEST_SIZE);
// 分配对象
for (size_t i = 0; i < TEST_SIZE; ++i) {
objects.emplace_back(i, i * 1.0, "test");
}
// 释放对象(自动)
});
std::cout << "ObjectPool time: " << pool_time << " ms" << std::endl;
std::cout << "std::vector time: " << vector_time << " ms" << std::endl;
std::cout << "Speedup: " << (vector_time / pool_time) << "x" << std::endl;
std::cout << "=========================================" << std::endl;
// 测试内存碎片率
std::cout << "Testing memory fragmentation..." << std::endl;
double pool_frag_time = measure_time([&]() {
hhb::core::ObjectPool<TestStruct> pool;
std::vector<TestStruct*> objects;
objects.reserve(TEST_SIZE);
// 分配一半对象
for (size_t i = 0; i < TEST_SIZE / 2; ++i) {
TestStruct* obj = pool.allocate();
new (obj) TestStruct(i, i * 1.0, "test");
objects.push_back(obj);
}
// 释放一半对象
for (size_t i = 0; i < objects.size() / 2; ++i) {
objects[i]->~TestStruct();
pool.deallocate(objects[i]);
}
// 重新分配对象
for (size_t i = 0; i < TEST_SIZE / 2; ++i) {
TestStruct* obj = pool.allocate();
new (obj) TestStruct(i + TEST_SIZE / 2, (i + TEST_SIZE / 2) * 1.0, "test");
objects.push_back(obj);
}
// 释放所有对象
for (TestStruct* obj : objects) {
obj->~TestStruct();
pool.deallocate(obj);
}
});
double vector_frag_time = measure_time([&]() {
std::vector<TestStruct> objects;
objects.reserve(TEST_SIZE);
// 分配一半对象
for (size_t i = 0; i < TEST_SIZE / 2; ++i) {
objects.emplace_back(i, i * 1.0, "test");
}
// 释放一半对象
objects.erase(objects.begin(), objects.begin() + objects.size() / 2);
// 重新分配对象
for (size_t i = 0; i < TEST_SIZE / 2; ++i) {
objects.emplace_back(i + TEST_SIZE / 2, (i + TEST_SIZE / 2) * 1.0, "test");
}
});
std::cout << "ObjectPool fragmentation test time: " << pool_frag_time << " ms" << std::endl;
std::cout << "std::vector fragmentation test time: " << vector_frag_time << " ms" << std::endl;
std::cout << "Fragmentation speedup: " << (vector_frag_time / pool_frag_time) << "x" << std::endl;
return 0;
}
|