Reputation:
I tried using Boost.Pool allocator with vector<wstring>
, expecting some form of performance gain over ordinarily-allocated vector<wstring>
(I was expecting some kind of fast results like these).
However, it seems that with Boost.Pool I'm actually getting worse results, e.g.:
for a count of 15,000 iterations, 0 ms
is displayed for ordinarily-allocated vector<wstring>
, instead the elapsed time using Boost.Pool is 5900 ms;
for a count of 5,000,000 iterations it takes about 1300 ms to complete the loop with the default allocator, instead with boost::pool_allocator
it takes lots of time (after a minute I broke with Ctrl+C).
Here is the C++ code benchmark I wrote:
//////////////////////////////////////////////////////////////////////////
// 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;
}
Am I misusing Boost.Pool? What am I missing here?
(I'm using VS2010 SP1 with Boost 1.49.0)
Upvotes: 3
Views: 2938
Reputation: 126
FYI Boost.Pool isn't designed for or optimised for that usage - it's designed for lots of fixed-sized blocks, as happens in a list (or even a map or a set), it's not really designed for fast performance with variable sized blocks as happens in a string or a vector.
Upvotes: 11