'boost::pool_allocator significantly slower than std::allocator
I am learning about memory pool, and trying to utilize boost::pool_allocator in my project. According to the documment, I made a small test about time cost:
template <typename Alloc>
void test()
{
using namespace std::chrono;
auto t0 = high_resolution_clock::now();
for (int i = 0; i < 1000; ++i) {
std::vector<int, Alloc> vec;
for (int j = 0; j < 10000; ++j)
vec.push_back(i + j);
}
auto t1 = high_resolution_clock::now();
auto time_ms = duration<double>(t1 - t0).count() * 1e3;
cout << "time cost: " << time_ms << " ms" << endl;
}
int main()
{
test<std::allocator<int>>();
test<boost::pool_allocator<int>>();
}
And the result is:
time cost: 3.97602 ms
time cost: 91.3943 ms
The Boost doc says:
Pools are generally used when there is a lot of allocation and deallocation of small objects.
So I expect boost::pool_allocator cost less time than std::allocator in the code above, but the test result shows it's much worse.
Am I using boost::pool_allocator wrong? In what situation can I obtain a speedup by using memory pool (or just Boost pool/pool_allocator)?
Solution 1:[1]
Pools are generally used when there is a lot of allocation and deallocation of small objects.
vector does not allocate "small objects". vector<T> allocates arrays. Or more specifically, a single array allocated in a slab of memory big enough to hold (at least) sizeof(T) * size bytes.
Pool allocators are fantastically bad at this allocation pattern.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nicol Bolas |
