c++ - 将 vector<wstring> 与 Boost.Pool 分配器一起使用

标签 c++ performance boost

我尝试将 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/

相关文章:

python - 在遵循 boost python 的逐步指导时引发了 boost python 错误

c++ - 在没有提升的情况下拆分 c++ 字符串,而不是在空格上

关于 vector 和 for/while 循环的 C++ 新手

javascript - 当键不存在时对象键查找速度变慢

mysql - 如何使用组合索引进行昂贵的聚合查询?

c++ - Python Pandas 与 C++ 文本 CSV 数据导入解决方案的性能对比

c++ - curl_easy_perform() 是同步的还是异步的?

c++ - 是否可以从 win32 或 MFC 应用程序中的窗口获取 "Low Disk Space"通知?

c++ - 将 boost::lexical_cast 与算术表达式一起使用

c++ - 使用 RAW 指针对 boost::shared_ptr 变量进行条件初始化