简体   繁体   English

NSSet vs.unordered_set

[英]NSSet vs. unordered_set

I performed a simple randomized test of the performance of Cocoa's NSSet vs. the STL's unordered_set . 我对Cocoa的NSSet和STL的unordered_set的性能进行了简单的随机测试。 Both are implemented using hash tables. 两者都是使用哈希表实现的。 Being that C++ is known for its highly performant code, I was surprised to see that NSSet was ~1.5x faster than unordered_set . 由于C ++以其高性能的代码而闻名,我很惊讶地看到NSSetunordered_set快约1.5倍。 Could anyone shed some light on why this is? 任何人都可以阐明为什么会这样吗?

PS The test was searching random strings (using NSString for NSSet and basic_string<unichar> for unordered_set ). PS该测试正在搜索随机字符串(对NSSet使用NSString ,对unordered_set使用basic_string<unichar> )。

EDIT: Here's the code: https://github.com/fumoboy007/StringCollectionSpeedTest . 编辑:这是代码: https : //github.com/fumoboy007/StringCollectionSpeedTest

Enable optimization and enjoy how "fast" C++ containers are: zero nano seconds for the loop, that has been optimized away by the compiler! 启用优化并享受C ++容器的“快速”状态:循环零纳米秒,已被编译器优化! Didn't you here it mumbling: "Don't mess with me!" 您不是在这里喃喃地说:“别惹我!” Compilers are smarter than you think. 编译器比您想象的要聪明。 ;) ;)

So, first trick the compiler: Restructure your test, so that a result will be generated by the loop, and that result MUST depend on the actions in each iteration (for example sum the count). 因此,首先欺骗编译器:调整你的测试,这样的结果将被循环产生,而结果必然取决于每个迭代(例如求和计数)的行动。 That result must be used as well somewhere, for example print it to the console. 该结果也必须在某处使用,例如将其打印到控制台。 Otherwise the compiler will again detect that you don't need that result and thus, don't need to perform the loop at all. 否则,编译器将再次检测到您不需要该结果,因此根本不需要执行循环。

Edit: 编辑:

Test Results (on my system): 测试结果(在我的系统上):

It turned out that NSSet and std::unordered_set are about equal in the time for a lookup. 事实证明,NSSet和std :: unordered_set的查找时间大致相等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM