Skip to content

Commit

Permalink
Merge pull request #3443 from pleroy/Benchmark
Browse files Browse the repository at this point in the history
A benchmark for adding to a PCP tree
  • Loading branch information
pleroy authored Oct 10, 2022
2 parents 471d1e6 + 5ce615e commit 07b97b8
Showing 1 changed file with 53 additions and 5 deletions.
58 changes: 53 additions & 5 deletions benchmarks/nearest_neighbour.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,27 @@ using geometry::Vector;
using World = Frame<enum class WorldTag>;
using V = Vector<double, World>;

PrincipalComponentPartitioningTree<V> BuildTree(
PrincipalComponentPartitioningTree<V> BuildTreeUsingAdd(
std::int64_t const points_in_tree,
std::int64_t const max_values_per_cell,
std::vector<V>& values) {
std::mt19937_64 random(42);
std::uniform_real_distribution<double> coordinate_distribution(-10, 10);

PrincipalComponentPartitioningTree<V> tree({}, max_values_per_cell);
std::vector<not_null<V const*>> pointers;
values.reserve(points_in_tree); // To avoid pointer invalidation below.
values.clear();
for (int i = 0; i < points_in_tree; ++i) {
values.push_back(V({coordinate_distribution(random),
coordinate_distribution(random),
coordinate_distribution(random)}));
tree.Add(&values.back());
}
return tree;
}

PrincipalComponentPartitioningTree<V> BuildTreeUsingConstructor(
std::int64_t const points_in_tree,
std::int64_t const max_values_per_cell,
std::vector<V>& values) {
Expand All @@ -39,14 +59,25 @@ PrincipalComponentPartitioningTree<V> BuildTree(
return PrincipalComponentPartitioningTree<V>(pointers, max_values_per_cell);
}

void BM_PCPBuildTree(benchmark::State& state) {
void BM_PCPBuildTreeUsingAdd(benchmark::State& state) {
std::int64_t const points_in_tree = state.range(0);
std::int64_t const max_values_per_cell = state.range(1);
std::vector<V> values;

for (auto _ : state) {
benchmark::DoNotOptimize(
BuildTreeUsingAdd(points_in_tree, max_values_per_cell, values));
}
}

void BM_PCPBuildTreeUsingConstructor(benchmark::State& state) {
std::int64_t const points_in_tree = state.range(0);
std::int64_t const max_values_per_cell = state.range(1);
std::vector<V> values;

for (auto _ : state) {
benchmark::DoNotOptimize(
BuildTree(points_in_tree, max_values_per_cell, values));
BuildTreeUsingConstructor(points_in_tree, max_values_per_cell, values));
}
}

Expand All @@ -56,7 +87,8 @@ void BM_PCPFindNearestNeighbour(benchmark::State& state) {
std::vector<V> values;
std::mt19937_64 random(42);
std::uniform_real_distribution<double> coordinate_distribution(-10, 10);
auto const tree = BuildTree(points_in_tree, max_values_per_cell, values);
auto const tree =
BuildTreeUsingConstructor(points_in_tree, max_values_per_cell, values);

for (auto _ : state) {
benchmark::DoNotOptimize(
Expand All @@ -66,7 +98,23 @@ void BM_PCPFindNearestNeighbour(benchmark::State& state) {
}
}

BENCHMARK(BM_PCPBuildTree)
BENCHMARK(BM_PCPBuildTreeUsingAdd)
->Args({1'000, 1})
->Args({1'000, 4})
->Args({1'000, 16})
->Args({1'000, 64})
->Args({1'000, 256})
->Args({10'000, 1})
->Args({10'000, 4})
->Args({10'000, 16})
->Args({10'000, 64})
->Args({10'000, 256})
->Args({100'000, 1})
->Args({100'000, 4})
->Args({100'000, 16})
->Args({100'000, 64})
->Args({100'000, 256});
BENCHMARK(BM_PCPBuildTreeUsingConstructor)
->Args({1'000, 1})
->Args({1'000, 4})
->Args({1'000, 16})
Expand Down

0 comments on commit 07b97b8

Please sign in to comment.