我尝试使用Boost.Pool分配器有vector<wstring>,希望在通常分配某种形式的性能增益vector<wstring>(我期待某种快速的结果像这些).
然而,似乎有了Boost.Pool,我实际上会得到更糟糕的结果,例如:
对于计数为15,000次的迭代,0 ms显示为通常分配vector<wstring>,而使用Boost.Pool的经过时间为5900 ms;
对于5,000,000次迭代的计数,使用默认分配器完成循环需要大约1300 ms,而boost::pool_allocator需要花费大量时间(一分钟后我用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;
}
Run Code Online (Sandbox Code Playgroud)
我在滥用Boost.Pool吗?我在这里错过了什么?
(我正在使用VS2010 SP1和Boost 1.49.0)
小智 11
FYI Boost.Pool不是为这种用途而设计或优化的 - 它是为许多固定大小的块而设计的,就像在列表中(甚至是地图或集合)一样,它并不是真正设计用于可变大小的快速性能在字符串或向量中发生的块.