Last active
May 5, 2021 07:01
-
-
Save eklitzke/5e7facd8b85b8bdfad8c2a2ea069f58c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "absl/container/flat_hash_map.h" | |
#include "absl/container/node_hash_map.h" | |
#include "benchmark/benchmark.h" | |
#include <map> | |
#include <unordered_map> | |
#include <vector> | |
// std::vector benchmark | |
#define PREINCREMENT_LIST(typename, var) \ | |
static void BM_PreIncrement##typename(benchmark::State & state) { \ | |
for (auto _ : state) { \ | |
unsigned sum = 0; \ | |
for (auto it = var.begin(); it != var.end(); ++it) { \ | |
benchmark::DoNotOptimize(sum += *it); \ | |
} \ | |
} \ | |
} \ | |
BENCHMARK(BM_PreIncrement##typename) | |
#define POSTINCREMENT_LIST(typename, var) \ | |
static void BM_PostIncrement##typename(benchmark::State & state) { \ | |
for (auto _ : state) { \ | |
unsigned sum = 0; \ | |
for (auto it = var.begin(); it != var.end(); it++) { \ | |
benchmark::DoNotOptimize(sum += *it); \ | |
} \ | |
} \ | |
} \ | |
BENCHMARK(BM_PostIncrement##typename) | |
using Vector = std::vector<unsigned>; | |
template <typename T> | |
static T IotaVector(size_t size) { | |
T vec(size); | |
std::iota(vec.begin(), vec.end(), 0); | |
return vec; | |
} | |
constexpr size_t kVectorSize = 1'000'000; | |
static const auto gVector = IotaVector<Vector>(kVectorSize); | |
PREINCREMENT_LIST(Vector, gVector); | |
POSTINCREMENT_LIST(Vector, gVector); | |
using ForwardList = std::forward_list<unsigned>; | |
static const auto gForwardList = IotaVector<ForwardList>(kVectorSize); | |
PREINCREMENT_LIST(ForwardList, gForwardList); | |
POSTINCREMENT_LIST(ForwardList, gForwardList); | |
using DoubleList = std::list<unsigned>; | |
static const auto gDoubleList = IotaVector<DoubleList>(kVectorSize); | |
PREINCREMENT_LIST(DoubleList, gDoubleList); | |
POSTINCREMENT_LIST(DoubleList, gDoubleList); | |
#define PREINCREMENT_MAP(typename, var) \ | |
static void BM_PreIncrement##typename(benchmark::State & state) { \ | |
for (auto _ : state) { \ | |
unsigned sum = 0; \ | |
for (auto it = var.begin(); it != var.end(); ++it) { \ | |
benchmark::DoNotOptimize(sum += it->second); \ | |
} \ | |
} \ | |
} \ | |
BENCHMARK(BM_PreIncrement##typename) | |
#define POSTINCREMENT_MAP(typename, var) \ | |
static void BM_PostIncrement##typename(benchmark::State & state) { \ | |
for (auto _ : state) { \ | |
unsigned sum = 0; \ | |
for (auto it = var.begin(); it != var.end(); it++) { \ | |
benchmark::DoNotOptimize(sum += it->second); \ | |
} \ | |
} \ | |
} \ | |
BENCHMARK(BM_PostIncrement##typename) | |
template <typename T> | |
T IotaMap(size_t size) { | |
T nums; | |
for (size_t i = 0; i < size; i++) { | |
nums.emplace(i, i); | |
} | |
return nums; | |
} | |
// std::map benchmark | |
using Map = std::map<unsigned, unsigned>; | |
constexpr size_t kMapSize = 1'000'000; | |
static const auto gMap = IotaMap<Map>(kMapSize); | |
PREINCREMENT_MAP(Map, gMap); | |
POSTINCREMENT_MAP(Map, gMap); | |
// std::unordered_map benchmark | |
using UnorderedMap = std::map<unsigned, unsigned>; | |
static const auto gUnorderedMap = IotaMap<UnorderedMap>(kMapSize); | |
PREINCREMENT_MAP(UnorderedMap, gUnorderedMap); | |
POSTINCREMENT_MAP(UnorderedMap, gUnorderedMap); | |
// absl::node_hash_map benchmark | |
using NodeHashMap = absl::node_hash_map<unsigned, unsigned>; | |
static const auto gNodeHashMap = IotaMap<NodeHashMap>(kMapSize); | |
PREINCREMENT_MAP(AbslNodeHashMap, gNodeHashMap); | |
POSTINCREMENT_MAP(AbslNodeHashMap, gNodeHashMap); | |
// absl::flat_hash_map benchmark | |
using FlatHashMap = absl::flat_hash_map<unsigned, unsigned>; | |
static const auto gFlatHashMap = IotaMap<FlatHashMap>(kMapSize); | |
PREINCREMENT_MAP(AbslFlatHashMap, gFlatHashMap); | |
POSTINCREMENT_MAP(AbslFlatHashMap, gFlatHashMap); | |
// Run the benchmark | |
BENCHMARK_MAIN(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment