Boost池分配器比新的慢

 mmmmmmmmmm0000 发布于 2022-12-15 14:49

所以我memory_pools基于boost池创建了这个容器分配器类:

memory_pools.hpp

#ifndef MEMORY_POOL_HPP
# define MEMORY_POOLS_HPP

// boost
# include 
# include 

template
class   memory_pools
{
public:
  template 
  friend class memory_pools;

private:
  using pool = boost::pool<>;

public:
  using value_type = ElementType;
  using pointer = value_type*;
  using const_pointer = const value_type*;
  using reference = value_type&;
  using const_reference = const value_type&;
  using size_type = pool::size_type;
  using difference_type = pool::difference_type;

public:

  template
  struct rebind
  {
    using other = memory_pools;
  };

public:
  memory_pools();

  template
  memory_pools(const memory_pools&);

public:
  pointer   allocate(const size_type n);
  void  deallocate(const pointer ptr, const size_type n);

  template
  void  construct(pointer, Args...);
  void  destroy(pointer);

public:
  bool  operator==(const memory_pools&);
  bool  operator!=(const memory_pools&);

private:
  using pools_map = boost::unordered_map>;

private:
  std::shared_ptr      pools_map_;
  std::shared_ptr           pool_;
};

# include 

#endif

memory_pools.ipp

#ifndef MEMORY_POOLS_IPP
# define MEMORY_POOLS_IPP

template
memory_pools::memory_pools()
  :
  pools_map_(std::make_shared
             (pools_map
             {
               std::make_pair
                 (sizeof(ElementType),
                  make_shared(sizeof(ElementType)))
             })),
  pool_(pools_map_->at(sizeof(ElementType)))
{
}

template
template
memory_pools::memory_pools
(const memory_pools& rebinded_from)
  :
  pools_map_(rebinded_from.pools_map_),
  pool_(pools_map_->insert
        (std::make_pair(sizeof(ElementType),
                        make_shared(sizeof(ElementType)))).first->second)
  {
  }

template
typename memory_pools::pointer memory_pools::allocate
(const size_type n)
{
  pointer ret = static_cast(pool_->ordered_malloc(n));

  if ((!ret) && n)
    throw std::bad_alloc();

  return (ret);
}

template
void        memory_pools::deallocate
(const pointer ptr, const size_type n)
{
  pool_->ordered_free(ptr, n);
}

template
template
void        memory_pools::construct(pointer ptr, Args... args)
{
  new (ptr) ElementType(std::forward(args)...);
}

template
void        memory_pools::destroy(pointer ptr)
{
  ptr->~ElementType();
}

template
bool        memory_pools::operator==(const memory_pools& rhs)
{
  return (pools_map_ == rhs.pools_map_);
}

template
bool        memory_pools::operator!=(const memory_pools& rhs)
{
  return (pools_map_ != rhs.pools_map_);
}

#endif

然后我用它测试它:

#include 

int     main(void)
{
  using pools_type = memory_pools>;
  pools_type    pools;

  boost::unordered_map, std::equal_to, pools_type>      map;
  //boost::unordered_map, std::equal_to>      map;

  for (unsigned int i = 0; i < 20000; ++i)
    {
      map[i] = i + 1;
    }

  return (0);
}

使用macOSX 10.10上的clang3.5,我得到了:

$ time ./a.out

real    0m1.873s
user    0m1.850s
sys     0m0.009s

而我发布的时候:

#include 

int     main(void)
{
  using pools_type = memory_pools>;
  pools_type    pools;

  //boost::unordered_map, std::equal_to, pools_type>      map;
  boost::unordered_map, std::equal_to>      map;

  for (unsigned int i = 0; i < 20000; ++i)
    {
      map[i] = i + 1;
    }

  return (0);
}

我有:

$ time ./a.out

real    0m0.019s
user    0m0.016s
sys     0m0.002s

使用boost pool的内存分配应该是那么慢还是我的测试由于某种原因无效?


编辑

在Carmeron的评论之后,我添加了-O3-DNDEBUG旗帜,现在我有:

$time ./a.out

real    0m0.438s
user    0m0.431s
sys     0m0.003s

对于memory_pools版本,和:

$ time ./a.out

real    0m0.008s
user    0m0.006s
sys     0m0.002s

对于标准分配器版本.

这个问题仍然存在,它是否正常更慢?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有