在 C++0x 标准中会有 unordered_map,这与 boosts unordered_map 相比如何?
哪个更有效率?有什么好的基准吗?
Which is more efficient? Are there any good benchmarks out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
哪个更有效率?有什么好的基准吗?
Which is more efficient? Are there any good benchmarks out?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
C++11 的 std::unordered_map 规范类似于基于 tr1::unordered_map 的 boost::unordered_map。话虽这么说,但还是有一些细微的差别。 C++11 中添加的右值引用导致添加了 emplace 和 emplace_hint 函数,这可能对性能有用。
C++11 现已广泛实现,因此您应该能够开箱即用地使用 std::unordered_map。 C++14 不会对其进行重大更改,C++17 将(可能)添加 insert_or_assign 和 try_emplace 成员函数。
C++11's std::unordered_map specification is similar to boost::unordered_map which is based on tr1::unordered_map. That being said, there are some small differences. The addition of rvalue references in C++11 result in the addition of the emplace and emplace_hint functions that may be useful for performance.
C++11 is now widely implemented and so you should be able to use std::unordered_map out of the box. C++14 does not change it significantly and C++17 will (probably) add the insert_or_assign and try_emplace member functions.
在c++0x最新标准草案n3225中,有一个第23.6.1节类模板unordered_map。
所以它已经在那里了。
C++0x unordered_map是在boost一的基础上提出的。 Boost库本身也有一个命名空间tr1::unordered_map,它共享自己的boost::unordered_map的实现。
如果你想比较(当然不需要比较boost和boost),我认为其他几个编译器,包括microsoft Visual Studio 2010和gcc,确实有自己的unordered_map实现。您可以假设它们位于命名空间 tr1 下来使用它们。
我还不知道任何基准测试,但我认为在这个早期阶段,任何基准测试都没有意义,因为当真正的标准最终确定并且更多的人将使用该库时,编译器实现者肯定会优化自己的实现。
In the c++0x latest standard draft n3225, there's a section 23.6.1 class template unordered_map.
So it is already there.
C++0x unordered_map is proposed based on boost one. Boost library itself also has a namespace tr1::unordered_map, which shares the implementation of its own boost::unordered_map.
If you want to compare (of course you don't need to compare boost with boost), I think several other compilers, including microsoft visual studio 2010, and gcc, do have their own unordered_map implementation. You can use them by assuming they are under namespace tr1.
I didn't know any benchmark yet but I think at this early time, any benchmarking doesn't make sense because the compiler implementer will definitely optimize their own implementations when the real standard is finalized and more people are going to use the library.
尚未提及的一个小问题是,std::hash 函数仅需要能够计算内置类型和字符串(以及一些其他类型)的哈希值。
boost::hash
函数可以计算更复杂对象的哈希值,例如pair
和tuple
。 boost 还有一个 hash_combine 函数来帮助为用户定义的类型创建哈希。这意味着 std::unordered_set
std::unordered_set<对>
不会编译,但boost::unordered_set
对>
将会。如果需要,您可以将
boost::hash
与std::unordered_*
一起使用。(参考:库扩展技术报告中的第 6.18 条问题列表。)
One minor point not yet mentioned, the
std::hash
function is only required to be able to compute hashes of builtin types and strings (and a few other types). Theboost::hash
function can compute hashes of more complex objects such aspair
andtuple
. Also boost has ahash_combine
function to assist in creating hashes for user-defined types.This means that
std::unordered_set< pair<int, int> >
won't compile, butboost::unordered_set< pair<int, int> >
will.You can use
boost::hash
withstd::unordered_*
if needed.(Reference: Item 6.18 in the Library Extension Technical Report Issues List.)
这取决于相关的实施和数据集。当我使用
unordered_map
来实现 博客文章 我发现 VS10 的std::unordered_map
的性能比boost::unordered_map
差得多对于我使用的输入 (我没有建立一个彻底的基准)。理论上认为不应该有区别。It depends on the implementation and the data set in question. When I was playing around with
unordered_map
for a blog post I found that VS10'sstd::unordered_map
perfromed much worse thanboost::unordered_map
for the input I used (I didn't build a thorough benchmark). In theory thought there shouldn't be a difference.