Add averaging to nearest neighbor benchmark to produce smoother plots

This commit is contained in:
Jackson Campolattaro 2021-01-18 12:16:30 -05:00
parent 31e669cece
commit 375cdf71ae
3 changed files with 81 additions and 60 deletions

View File

@ -29,6 +29,8 @@ using std::chrono::microseconds;
int main(int argc, char **argv) {
int num_runs = 100;
size_t k = 10;
// Set output file
@ -36,11 +38,19 @@ int main(int argc, char **argv) {
file.open((argc > 1) ? argv[1] : "../nearest_neighbor_benchmark.csv");
// Add header for CSV
file << "Number of Points,Octree,kDTree,Naive \n";
file << "Number of Points,Octree,kDTree \n";
// Perform tests for various dataset sizes
for (size_t num_points = 100; num_points < 100000; num_points *= 1.05) {
// We want the average of several runs for each point count, for cleaner results
float octreeAverage = 0;
float kdtreeAverage = 0;
float naiveAverage = 0;
// Repeat the tests, generating a new point set for each run
for (int i = 0; i < num_runs; ++i) {
// Create a collection of the right number of points
auto points = generate<Kernel>(num_points);
@ -59,32 +69,32 @@ int main(int argc, char **argv) {
);
// Time how long it takes to find neighbors using a naive approach
auto naiveTime = bench<microseconds>(
[&] {
std::vector<Point> nearest_neighbors;
// Iterate over every point
for (auto &point : points.points()) {
// Find out how this point ranks in comparison with other points we've saved
auto iter = nearest_neighbors.begin();
for (; iter < nearest_neighbors.end() &&
CGAL::squared_distance(point, search_point) <
CGAL::squared_distance(*iter, search_point);
iter++) {}
// Add the point to the list (it'll usually be at the end)
nearest_neighbors.insert(iter, point);
// Never keep more than k neighbors
if (nearest_neighbors.size() > k)
nearest_neighbors.resize(k);
}
}
);
// auto naiveTime = bench<microseconds>(
// [&] {
//
// std::vector<Point> nearest_neighbors;
//
// // Iterate over every point
// for (auto &point : points.points()) {
//
// // Find out how this point ranks in comparison with other points we've saved
// auto iter = nearest_neighbors.begin();
// for (; iter < nearest_neighbors.end() &&
// CGAL::squared_distance(point, search_point) <
// CGAL::squared_distance(*iter, search_point);
// iter++) {}
//
// // Add the point to the list (it'll usually be at the end)
// nearest_neighbors.insert(iter, point);
//
// // Never keep more than k neighbors
// if (nearest_neighbors.size() > k)
// nearest_neighbors.resize(k);
//
// }
//
// }
// );
// Build the octree from points (this had to be done second because it rearranges the point set)
Octree octree(points, points.point_map());
@ -98,10 +108,20 @@ int main(int argc, char **argv) {
}
);
// Incorporate our results into the average
octreeAverage += (float) octreeTime.count() / (float) num_runs;
kdtreeAverage += (float) kdtreeTime.count() / (float) num_runs;
// naiveAverage += (float) naiveTime.count() / (float) num_runs;
// A simple progress indication
std::cout << ".";
}
file << num_points << ",";
file << octreeTime.count() << ",";
file << kdtreeTime.count() << ",";
file << naiveTime.count() << "\n";
file << octreeAverage << ",";
file << kdtreeAverage << ",";
// file << naiveAverage << ",";
file << "\n";
std::cout << num_points << std::endl;

View File

@ -14,4 +14,4 @@ set ylabel "Time (us)"
set key autotitle columnhead
set datafile separator ","
plot for [col=2:4] 'nearest_neighbor_benchmark.csv' using 1:col
plot for [col=2:10] 'nearest_neighbor_benchmark.csv' using 1:col

View File

@ -26,19 +26,20 @@ CGAL::Point_set_3<typename Kernel::Point_3> generate(size_t num_points = 1) {
}
template<typename Time_unit>
Time_unit bench(const std::function<void(void)>& f) {
Time_unit bench(const std::function<void(void)> &f, size_t repetitions = 1) {
// Start the timer
auto start = std::chrono::high_resolution_clock::now();
// Run the function being benchmarked
// Run the function being benchmarked as many times as requested
for (int i = 0; i < repetitions; ++i)
f();
// End the timer
auto end = std::chrono::high_resolution_clock::now();
// Return the elapsed time
return std::chrono::duration_cast<Time_unit>(end - start);
return std::chrono::duration_cast<Time_unit>(end - start) / repetitions;
}
#endif //OCTREE_UTIL_H