问题是您选择的集合太小。
这是一个具有1000个元素的示例。
#include <unordered_set>
#include <set>
const static std::unordered_set<int> mySet {
0,
1,
2,
...
998,
999
};
static void UsingSetFind(benchmark::State& state) {
// Code inside this loop is measured repeatedly
for (auto _ : state) {
auto it = mySet.find(435);
benchmark::DoNotOptimize(it);
}
}
// Register the function as a benchmark
BENCHMARK(UsingSetFind);
static void UsingStdFind(benchmark::State& state) {
// Code before the loop is not measured
for (auto _ : state) {
auto it = std::find(mySet.begin(), mySet.end(), 345);
benchmark::DoNotOptimize(it);
}
}
BENCHMARK(UsingStdFind);
区别是惊人的
mySet.find(435)
可以像搜索哈希表一样快速搜索它。而
std::find(mySet.begin(), mySet.end(), 345);
将乘以1。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句