- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试实现一个 boost::multi_index 应用程序,但性能非常糟糕:插入 10,000 个对象几乎需要 0.1 秒,这是 Not Acceptable 。因此,当我查看文档并发现 boost::multi_index 可以接受内存分配器作为最后一个参数时,但是当我尝试自己实现时遇到了很多编译错误。请帮我改正。谢谢。
struct order
{
unsigned int id;
unsigned int quantity;
double price;
};
struct id{};
struct price{};
typedef multi_index_container<
order,
indexed_by<
hashed_unique<
tag<id>, BOOST_MULTI_INDEX_MEMBER(order, unsigned int, id)>,
ordered_non_unique<
tag<price>,BOOST_MULTI_INDEX_MEMBER(order ,double, price),
std::less<double> >
>,
boost::object_pool<order>
> order_sell;
一般来说,编译器不喜欢在 order_sell 声明中将 boost::object_pool 表达为分配器。
最佳答案
让我重申一下 Alexander 的建议,即您分析程序以了解问题的真正所在。我强烈怀疑 Boost.MultiIndex 本身会像你说的那样慢。以下程序测量了创建 order_sell
容器(没有 Boost.Pool)、用 10,000 个随机订单填充它并销毁它所花费的时间:
#include <algorithm>
#include <array>
#include <chrono>
#include <numeric>
std::chrono::high_resolution_clock::time_point measure_start,measure_pause;
template<typename F>
double measure(F f)
{
using namespace std::chrono;
static const int num_trials=10;
static const milliseconds min_time_per_trial(200);
std::array<double,num_trials> trials;
volatile decltype(f()) res; /* to avoid optimizing f() away */
for(int i=0;i<num_trials;++i){
int runs=0;
high_resolution_clock::time_point t2;
measure_start=high_resolution_clock::now();
do{
res=f();
++runs;
t2=high_resolution_clock::now();
}while(t2-measure_start<min_time_per_trial);
trials[i]=duration_cast<duration<double>>(t2-measure_start).count()/runs;
}
(void)res; /* var not used warn */
std::sort(trials.begin(),trials.end());
return std::accumulate(
trials.begin()+2,trials.end()-2,0.0)/(trials.size()-4);
}
void pause_timing()
{
measure_pause=std::chrono::high_resolution_clock::now();
}
void resume_timing()
{
measure_start+=std::chrono::high_resolution_clock::now()-measure_pause;
}
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
using namespace boost::multi_index;
struct order
{
unsigned int id;
unsigned int quantity;
double price;
};
struct id{};
struct price{};
typedef multi_index_container<
order,
indexed_by<
hashed_unique<
tag<id>,BOOST_MULTI_INDEX_MEMBER(order, unsigned int, id)>,
ordered_non_unique<
tag<price>,BOOST_MULTI_INDEX_MEMBER(order ,double, price),
std::less<double> >
>
> order_sell;
#include <iostream>
#include <random>
int main()
{
std::cout<<"Insertion of 10,000 random orders plus container cleanup\n";
std::cout<<measure([](){
order_sell os;
std::mt19937 gen{34862};
std::uniform_int_distribution<unsigned int> uidist;
std::uniform_real_distribution<double> dbdist;
for(unsigned int n=0;n<10000;++n){
os.insert(order{uidist(gen),0,dbdist(gen)});
}
return os.size();
})<<" seg.\n";
}
当 Coliru 使用任何后端以 -O3
模式运行时,我们得到:
Insertion of 10,000 random orders plus container cleanup0.00494657 seg.
我的机器(Intel Core i5-2520M @2.50GHz)中的 VS 2015 Release模式产生:
Insertion of 10,000 random orders plus container cleanup0.00492825 seg.
因此,这比您报告的快 20 倍左右,而且我在测量中包括容器销毁和随机数生成。
一些额外的观察:
boost::object_pool
不提供标准库为与容器的互操作性指定的分配器接口(interface)。您可能想使用 boost::pool_allocator
取而代之(我已经玩了一会儿,但似乎并没有 boost 速度,但你的里程可能会有所不同)。order_sell
容器,每个值都进入其自己的一个节点,另外还有一个单独的所谓的桶数组(指针数组)长度与元素数量大致相同。对于基于节点的数据结构,没有比这更好的了(如果您想放弃迭代器的稳定性,您可以设计出内存效率更高的方案)。关于c++ - 如何使用 boost::object_pool 作为 boost::multi_index 分配器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37095641/
这是我的 multi_index 代码: struct tag_type {}; typedef boost::multi_index_container, b
class ObjectStorage { private: std::string objName; int zIndex; // Refer
2个问题: 1. 我真的需要通过成员将参数传递给 modify/modify_key 吗? 2. 为什么会出现这个编译错误 要查看包含错误的完整代码,您可以查看http://coliru.stacke
我有一个包含多个 hashed_unique 索引(A、B、C)的 multi_index。 并非所有对哈希的插入都包含所有索引的信息。一些索引可能是空的。有些可能包含所有条目。 我将它们定义为 ha
为了避免 msvc2010 编译器错误,我在 composite_key 中使用用户定义的 key 提取器,如下所示: enum NodeType { TypeOne = 0, T
首先我想展示工作代码,然后解释我想如何改变。这是简单的 boost multi_index 示例: //main.cpp #include #include #inc
我有一个带有多个索引的 boost multi_index 容器。如何使用迭代时指定的自定义比较来迭代元素。 例如,假设 Element::name 和 Element::index 由 multi_
所以。我正在使用 igraph 对象,我想以特定顺序迭代顶点。顺序由称为“值”的顶点属性确定,我想按从高到低的顺序进行操作。 igraph 可以按顶点 ID 顺序将所有值作为 igraph_vecto
我想按顺序搜索 boost::multi_index 容器并按顺序获取下一个元素。 下面的代码存储了四个具有不同索引(顺序和有序)的 float 。 最后一个 if 语句是问题所在。我不知道如何编辑以
我的应用程序中有一个 boost::multi_index 容器,它包含对象的共享指针 (std::shared_ptr)。正如我从文档中了解到的那样,它创建了具有对唯一索引的双向访问的树结构。是否可
阅读 boost::multi_index 引用文献,我发现 iterator_to 方法具有恒定顺序。这怎么可能?我的意思是,如果迭代器是一个不同于它所代表的 value_type 的对象,容器怎么
其定义如下: typedef boost::multi_index_container, boost::multi_index::member, boost::mult
我正在尝试制作一个 boost::multi_index 容器,它使用带参数的成员函数作为键。 class Data { public: std::string get(const std::s
我有一个 boost::multi_index 容器。谁能告诉我如何根据某个键检索一系列迭代器?经过几个小时的搜索,我想到 lower_bound 或 upper_bound 应该可以解决问题,但我仍
这个问题在这里已经有了答案: Where and why do I have to put the "template" and "typename" keywords? (8 个答案) 关闭 5
我正在应用 boost multi_index 示例的示例 6。 https://www.boost.org/doc/libs/1_71_0/libs/multi_index/doc/examples
我有以下(简化的)代码: #include #include namespace bmi = boost::multi_index; #include #include #include u
我想在内部实现 boost 多索引两组具有相同搜索条件但不同驱逐条件的键。假设我有两组具有相同搜索条件的数据,但一组需要 100 的 MRU(最近使用)列表,另一组需要 200 的 MRU。假设条目是
我有这个结构: struct myData { unsigned long id; int age; int phone; myData(){}; myDa
假设我有一个对象,可以通过来自不同 namespace 的不同名称来标识: enum Namespace { nspaceA, nspaceB }; struct Object {
我是一名优秀的程序员,十分优秀!