Files
conflict-set/Bench.cpp

108 lines
2.2 KiB
C++

#include "ConflictSet.h"
#include "Internal.h"
#define ANKERL_NANOBENCH_IMPLEMENT
#include "third_party/nanobench.h"
std::string toKey(int n) {
std::string result;
result.resize(32);
for (int i = 0; i < 32; ++i) {
result[i] = n & (1 << (31 - i)) ? '1' : '0';
}
return result;
}
constexpr int kNumKeys = 100000;
void benchPointRead() {
ankerl::nanobench::Bench bench;
int readKey = kNumKeys / 2;
ConflictSet cs(0);
for (int i = 0; i < kNumKeys; ++i) {
if (i != readKey) {
continue;
}
auto key = toKey(i);
ConflictSet::WriteRange w;
w.begin.p = (const uint8_t *)key.data();
w.begin.len = key.size();
w.end.len = 0;
w.writeVersion = 1;
cs.addWrites(&w, 1);
}
auto key = toKey(readKey);
ConflictSet::ReadRange r;
r.begin.p = (const uint8_t *)key.data();
r.begin.len = key.size();
r.end.len = 0;
r.readVersion = 0;
ConflictSet::Result result;
bench.run("point read", [&]() { cs.check(&r, &result, 1); });
}
void benchPointerSet() {
ankerl::nanobench::Bench bench;
constexpr int kNumPointers = 32;
bench.batch(kNumPointers);
std::vector<void *> pointers;
for (int i = 0; i < kNumPointers; ++i) {
pointers.push_back(malloc(1));
}
bench.run("Create and destroy Arena hashset", [&]() {
Arena arena;
auto h = hashSet<void *>(arena);
for (auto p : pointers) {
h.insert(p);
}
});
bench.run("Create and destroy malloc hashset", [&]() {
std::unordered_set<void *, MyHash<void *>> h;
for (auto p : pointers) {
h.insert(p);
}
});
bench.run("Create and destroy Arena vector", [&]() {
Arena arena;
auto h = vector<void *>(arena);
for (auto p : pointers) {
h.push_back(p);
}
});
{
Arena arena;
auto h = hashSet<void *>(arena);
bench.run("Find hashset", [&]() {
for (auto p : pointers) {
bench.doNotOptimizeAway(h.find(p));
}
});
}
{
bench.run("Find vector", [&]() {
for (auto p : pointers) {
bench.doNotOptimizeAway(std::find(pointers.begin(), pointers.end(), p));
}
});
}
for (auto p : pointers) {
free(p);
}
}
int main(void) {
// benchPointRead();
benchPointerSet();
}