user1149224
user1149224

Reputation:

Using vector<wstring> with Boost.Pool allocator

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.:

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

Answers (1)

John Maddock
John Maddock

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

Related Questions