【发布时间】:2010-05-14 00:29:59
【问题描述】:
我有一个 boost multi_index_container 声明如下,它由 hash_unique id(unsigned long) 和 hash_non_unique transaction id(long) 索引。元素的插入和检索速度很快,但是当我删除元素时,速度要慢得多。我希望它是恒定的时间,因为密钥是散列的。
例如从容器中删除元素
对于 10,000 个元素,大约需要 2.53927016 秒
对于 15,000 个元素,大约需要 7.137100068 秒
对于 20,000 个元素,大约需要 21.391720757 秒
这是我遗漏的东西还是预期的行为?
班级会议 { 民众: 会议() { //增加唯一id 静态无符号长计数器 = 0; boost::mutex::scoped_lock guard(mx); 计数器++; m_nId = 计数器; } 无符号长 GetId() { 返回 m_nId; } 长 GetTransactionHandle(){ 返回 m_nTransactionHandle; } …… 私人的: 无符号长 m_nId; 长 m_nTransactionHandle; boost::mutext mx; …… };typedef multi_index_container<
Session*,
indexed_by<
hashed_unique< mem_fun<Session,unsigned long,&Session::GetId> >,
hashed_non_unique< mem_fun<Session,unsigned long,&Session::GetTransactionHandle> >
> //end indexed_by
> SessionContainer;
typedef SessionContainer::nth_index<0>::type SessionById;
int main() {
...
SessionContainer container;
SessionById *pSessionIdView = &get<0>(container);
unsigned counter = atoi(argv[1]);
vector<Session*> vSes(counter);
//insert
for(unsigned i = 0; i < counter; i++) {
Session *pSes = new Session();
container.insert(pSes);
vSes.push_back(pSes);
}
timespec ts;
lock_settime(CLOCK_PROCESS_CPUTIME_ID, &ts);
//erase
for(unsigned i = 0; i < counter; i++) {
pSessionIdView->erase(vSes[i]->getId());
delete vSes[i];
}
lock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
std::cout << "Total time taken for erase:" << ts.tv_sec << "." << ts.tv_nsec << "\n";
return (EXIST_SUCCESS);
}
【问题讨论】:
-
你试过分别定时删除和擦除吗?
-
是的。删除不需要太多时间。事实上,我在内部使用 boost::object_pool 来构造和破坏对象。所以没有分配/解除分配的开销。为了简单起见,我只是在这里使用“删除”。
-
各种第二索引类型的性能测试结果链接。 joshitech.blogspot.com/2010/05/…
-
@rjoshi,性能测试结果在某处仍然可用吗?你的博客链接失效了。谢谢!
标签: boost