我尝试将 Boost.Pool 分配器 与 vector<wstring>
一起使用,期望比通常分配的性能获得某种形式的 vector<wstring>
(我期待像 these 这样的快速结果)。
但是,似乎使用 Boost.Pool 我实际上得到了更差的结果,例如:
对于 15,000 次迭代的计数,
0 ms
显示为普通分配vector<wstring>
,而不是使用 Boost.Pool 的耗时是 5900 毫秒;对于 5,000,000 次迭代,使用默认分配器(而不是
boost::pool_allocator
)完成循环大约需要 1300 毫秒这需要很多时间(一分钟后我用 Ctrl+C 中断了)。
这是我写的 C++ 代码基准:
//////////////////////////////////////////////////////////////////////////
// TestBoostPool.cpp
// Testing vector<wstring> with Boost.Pool
//////////////////////////////////////////////////////////////////////////
#include <exception>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
// To avoid linking with Boost Thread library
#define BOOST_DISABLE_THREADS
#include <boost/pool/pool_alloc.hpp>
#include <boost/timer/timer.hpp>
using namespace std;
void Test()
{
// Loop iteration count
static const int count = 5*1000*1000;
//
// Testing ordinary vector<wstring>
//
cout << "Testing vector<wstring>" << endl;
{
boost::timer::auto_cpu_timer t;
vector<wstring> vec;
for (int i = 0; i < count; i++)
{
wstring s(L"I think therefore I am; just a simple test string.");
vec.push_back(s);
}
}
//
// Testing vector<wstring> with Boost.Pool
//
cout << "Testing vector<wstring> with Boost.Pool" << endl;
{
boost::timer::auto_cpu_timer t;
typedef basic_string<wchar_t, char_traits<wchar_t>,
boost::fast_pool_allocator<wchar_t>> PoolString;
vector<PoolString> vec;
for (int i = 0; i < count; i++)
{
PoolString s(L"I think therefore I am; just a simple test string.");
vec.push_back(s);
}
// Release pool memory
boost::singleton_pool<boost::fast_pool_allocator_tag, sizeof(wchar_t)>::release_memory();
}
cout << endl;
}
int main()
{
const int exitOk = 0;
const int exitError = 1;
try
{
Test();
}
catch(const exception & e)
{
cerr << "\n*** ERROR: " << e.what() << endl;
return exitError;
}
return exitOk;
}
我是否滥用了 Boost.Pool?我在这里错过了什么?
(我正在使用 VS2010 SP1 和 Boost 1.49.0)
最佳答案
仅供引用 Boost.Pool 并非针对该用途而设计或优化 - 它是为许多固定大小的 block 而设计的,如列表(甚至 map 或集合)中发生的那样,它并不是真正为可变大小块的快速性能而设计的,就像在字符串或 vector 中发生的那样。
关于c++ - 将 vector<wstring> 与 Boost.Pool 分配器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10451585/