Permalink
Browse files

Update erroneous set lookup code

  • Loading branch information...
akshaynanavati committed Sep 5, 2018
1 parent 18bb25d commit e7de3899c9d631e605b16e21c2ad4c9d35167d40
Showing with 34 additions and 9 deletions.
  1. +1 −1 benchmarks/lookup_int.cpp
  2. +33 −8 benchmarks/lookup_string.cpp
@@ -29,7 +29,7 @@ void BM_set(benchmark::State &state) {
for (auto _ : state) {
state.PauseTiming();
Set<Int> s;
for (size_t i = 0; i < kLookups; ++i) {
for (auto i = 0; i < state.range(0); ++i) {
s.insert(rng());
}
state.ResumeTiming();
@@ -1,4 +1,5 @@
#include <algorithm>
#include <ctime>
#include <random>
#include <set>
#include <unordered_set>
@@ -9,24 +10,46 @@
constexpr size_t kLookups = 1 << 15;
constexpr size_t kMinSize = 1 << 5;
constexpr size_t kMaxSize = 1 << 15;
constexpr size_t kStringSize = 31;
constexpr size_t kStringSize = 16;
static std::mt19937 rng32{(uint32_t)time(NULL)};
std::string randString() {
static std::mt19937 rng32;
auto rngc = []() { return (char)(rng32() & 127); };
auto rngc = []() { return (char)(rng32() % 127 + 1); };
std::string s(kStringSize, 'a');
std::generate(s.begin(), s.end(), rngc);
return s;
}
std::vector<std::string> getKeysToLookup() {
std::vector<std::string> toLookup{kLookups};
std::generate(toLookup.begin(), toLookup.end(), randString);
return toLookup;
}
void BM_vector(benchmark::State &state) {
for (auto _ : state) {
state.PauseTiming();
std::vector<std::string> vec(state.range(0));
std::generate(vec.begin(), vec.end(), randString);
auto toLookup = getKeysToLookup();
state.ResumeTiming();
for (const auto &key : toLookup) {
benchmark::DoNotOptimize(std::find(vec.begin(), vec.end(), key));
}
}
}
void BM_vector_bin_search(benchmark::State &state) {
for (auto _ : state) {
state.PauseTiming();
std::vector<std::string> vec(state.range(0));
std::generate(vec.begin(), vec.end(), randString);
std::sort(vec.begin(), vec.end());
auto toLookup = getKeysToLookup();
state.ResumeTiming();
for (size_t i = 0; i < kLookups; ++i) {
benchmark::DoNotOptimize(std::find(vec.begin(), vec.end(), randString()));
for (const auto &key : toLookup) {
benchmark::DoNotOptimize(std::binary_search(vec.begin(), vec.end(), key));
}
}
}
@@ -35,12 +58,13 @@ template <template <typename> class Set> void BM_set(benchmark::State &state) {
for (auto _ : state) {
state.PauseTiming();
Set<std::string> s;
for (size_t i = 0; i < kLookups; ++i) {
for (auto i = 0; i < state.range(0); ++i) {
s.insert(randString());
}
auto toLookup = getKeysToLookup();
state.ResumeTiming();
for (int i = 0; i < kLookups; ++i) {
benchmark::DoNotOptimize(s.find(randString()));
for (const auto &key : toLookup) {
benchmark::DoNotOptimize(s.find(key));
}
}
}
@@ -52,5 +76,6 @@ BENCHMARK_TEMPLATE(BM_set, std::set)
BENCHMARK_TEMPLATE(BM_set, std::unordered_set)
->RangeMultiplier(2)
->Range(kMinSize, kMaxSize);
BENCHMARK(BM_vector_bin_search)->RangeMultiplier(2)->Range(kMinSize, kMaxSize);
BENCHMARK_MAIN();

0 comments on commit e7de389

Please sign in to comment.