清除vs交换的性能 [英] perfomance of clear vs swap
问题描述
您好,
我调用了hash_map :: clear()函数需要很长时间。
someClass :: someFunction ()
{
// typedef hash_map< name_id,uintMp;
// Mp p;
//假设正确的命名空间,name_id obj的哈希函数。
p.clear();
}
以上p.clear()需要很长时间,分析表明''桶数
哈希表很大''。
现在,仅仅为了实验,我用
swap替换了这个''clear''调用。 ie
someClass :: someFunction()
{
// typedef hash_map< name_id,uintMp;
// Mp p;
//假设正确的命名空间,name_is obj的哈希函数。
//p.clear( );
Mp tmp;
p.swap(tmp);
}
现在运行时间显着下降,减少10倍。
导致运行时间减少的原因是什么?
谢谢,
Krishanu
-
[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]
[comp.lang。 C ++。主持。第一次海报:做到这一点! ]
Hello,
I have a call to hash_map::clear() function which takes long time.
someClass::someFunction()
{
// typedef hash_map<name_id, uintMp;
// Mp p;
// assuming proper namespace, hash function for name_id obj.
p.clear();
}
Above p.clear() takes long time, profiling indicates ''number of bucket
of hash table is large''.
Now, just for sake of experiments, I replaced this ''clear'' call with
swap. i.e.
someClass::someFunction()
{
// typedef hash_map<name_id, uintMp;
// Mp p;
// assuming proper namespace, hash function for name_is obj.
//p.clear();
Mp tmp;
p.swap(tmp);
}
Now runtime drops significantly, 10 fold less.
What''s exactly cause this run time reduction?
Thanks,
Krishanu
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
推荐答案
Krishanu Debnath写道:
Krishanu Debnath wrote:
您好,
我调用了hash_map :: clear()函数需要很长时间。
Hello,
I have a call to hash_map::clear() function which takes long time.
嗯......惊讶于这让它变得适度。我已经拿出来了,因为我不想等待。
hash_map不是标准的,并且在每个实现上都是不同的/>
实现它。很难说速度的降低是由什么导致的。
Hmmm...surprised this made it into moderated. I''ve taken it out as I
don''t like to wait.
hash_map is not standard and is different on every implementation that
implements it. It''s hard to say what the reduction in speed is caused
by.
Noah Roberts写道:
Noah Roberts wrote:
Krishanu Debnath写道:
Krishanu Debnath wrote:
你好,
我有一个调用hash_map :: clear()函数需要很长时间。
Hello,
I have a call to hash_map::clear() function which takes long time.
嗯......惊讶于这让它变得适度。我已经拿出来了,因为我不想等待。
hash_map不是标准的,并且在每个实现上都是不同的/>
实现它。很难说速度降低的原因是什么?b
。
Hmmm...surprised this made it into moderated. I''ve taken it out as I
don''t like to wait.
hash_map is not standard and is different on every implementation that
implements it. It''s hard to say what the reduction in speed is caused
by.
但它是TR1的一部分为std :: tr1 :: unordered_map,符合
FAQ 5.9。
干杯! --M
But it is part of TR1 as std::tr1::unordered_map, which qualifies under
FAQ 5.9.
Cheers! --M
Krishanu Debnath写道:
Krishanu Debnath wrote:
我调用了hash_map :: clear()函数需要很长时间。
someClass :: someFunction()
{
// typedef hash_map< name_id,uintMp;
// Mp p;
//假设正确的命名空间,name_id obj的哈希函数。
p.clear();
}
高于p.clear()需要很长时间,分析表明''桶数
的哈希表很大''。
现在,为了实验,我用
替换了这个''clear''调用交换。 ie
someClass :: someFunction()
{
// typedef hash_map< name_id,uintMp;
// Mp p;
//假设正确的命名空间,name_is obj的哈希函数。
//p.clear( );
Mp tmp;
p.swap(tmp);
}
现在运行时间显着下降,减少10倍。
究竟是什么导致运行时间缩短?
I have a call to hash_map::clear() function which takes long time.
someClass::someFunction()
{
// typedef hash_map<name_id, uintMp;
// Mp p;
// assuming proper namespace, hash function for name_id obj.
p.clear();
}
Above p.clear() takes long time, profiling indicates ''number of bucket
of hash table is large''.
Now, just for sake of experiments, I replaced this ''clear'' call with
swap. i.e.
someClass::someFunction()
{
// typedef hash_map<name_id, uintMp;
// Mp p;
// assuming proper namespace, hash function for name_is obj.
//p.clear();
Mp tmp;
p.swap(tmp);
}
Now runtime drops significantly, 10 fold less.
What''s exactly cause this run time reduction?
我不知道为什么会这样。你在使用SGI的hash_map扩展名
(类似于std :: tr1 :: unordered_map)?你在测量整个功能的
性能吗?包括析构函数为/ b $ b自动对象如tmp?
干杯! --M
-
[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
I''m not sure why this would be. Are you using SGI''s hash_map extension
(which is similar to std::tr1::unordered_map)? Are you measuring the
performance of the entire function including the destructors for
automatic objects like tmp?
Cheers! --M
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
这篇关于清除vs交换的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!