voronoi_benchmark.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Boost.Polygon library voronoi_benchmark.cpp file
  2. // Copyright Andrii Sydorchuk 2010-2012.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org for updates, documentation, and revision history.
  7. #include <algorithm>
  8. #include <iomanip>
  9. #include <iostream>
  10. #include <fstream>
  11. #include <list>
  12. #include <numeric>
  13. #include <vector>
  14. #define BOOST_TEST_MODULE benchmark_test
  15. #include <boost/mpl/list.hpp>
  16. #include <boost/random/mersenne_twister.hpp>
  17. #include <boost/test/test_case_template.hpp>
  18. #include <boost/timer.hpp>
  19. #include <boost/polygon/polygon.hpp>
  20. #include <boost/polygon/voronoi.hpp>
  21. using boost::polygon::point_data;
  22. using boost::polygon::segment_data;
  23. using boost::polygon::voronoi_diagram;
  24. typedef boost::mpl::list<int> test_types;
  25. const char *BENCHMARK_FILE = "voronoi_benchmark.txt";
  26. const char *POINT_INPUT_FILE = "input_data/voronoi_point.txt";
  27. const char *SEGMENT_INPUT_FILE = "input_data/voronoi_segment.txt";
  28. const int POINT_RUNS = 10;
  29. const int SEGMENT_RUNS = 10;
  30. boost::mt19937 gen(static_cast<unsigned int>(time(NULL)));
  31. boost::timer timer;
  32. BOOST_AUTO_TEST_CASE_TEMPLATE(benchmark_test_random, T, test_types) {
  33. typedef T coordinate_type;
  34. typedef point_data<coordinate_type> point_type;
  35. voronoi_diagram<double> test_output;
  36. std::ofstream bench_file(BENCHMARK_FILE, std::ios_base::out | std::ios_base::app);
  37. bench_file << "Voronoi Benchmark Test (time in seconds):" << std::endl;
  38. bench_file << "| Number of points | Number of tests | Time per one test |" << std::endl;
  39. bench_file << std::setiosflags(std::ios::right | std::ios::fixed) << std::setprecision(6);
  40. int max_points = 100000;
  41. std::vector<point_type> points;
  42. coordinate_type x, y;
  43. for (int num_points = 10; num_points <= max_points; num_points *= 10) {
  44. points.resize(num_points);
  45. timer.restart();
  46. int num_tests = max_points / num_points;
  47. for (int cur = 0; cur < num_tests; cur++) {
  48. test_output.clear();
  49. for (int cur_point = 0; cur_point < num_points; cur_point++) {
  50. x = static_cast<coordinate_type>(gen());
  51. y = static_cast<coordinate_type>(gen());
  52. points[cur_point] = point_type(x, y);
  53. }
  54. construct_voronoi(points.begin(), points.end(), &test_output);
  55. }
  56. double elapsed_time = timer.elapsed();
  57. double time_per_test = elapsed_time / num_tests;
  58. bench_file << "| " << std::setw(16) << num_points << " ";
  59. bench_file << "| " << std::setw(15) << num_tests << " ";
  60. bench_file << "| " << std::setw(17) << time_per_test << " ";
  61. bench_file << "| " << std::endl;
  62. }
  63. bench_file.close();
  64. }
  65. BOOST_AUTO_TEST_CASE_TEMPLATE(benchmark_test_points, T, test_types) {
  66. typedef T coordinate_type;
  67. typedef point_data<coordinate_type> point_type;
  68. std::vector<point_type> points;
  69. {
  70. std::ifstream input_file(POINT_INPUT_FILE);
  71. int num_points;
  72. coordinate_type x, y;
  73. input_file >> num_points;
  74. points.reserve(num_points);
  75. for (int i = 0; i < num_points; ++i) {
  76. input_file >> x >> y;
  77. points.push_back(point_type(x, y));
  78. }
  79. input_file.close();
  80. }
  81. std::vector<double> periods;
  82. {
  83. for (int i = 0; i < POINT_RUNS; ++i) {
  84. voronoi_diagram<double> test_output;
  85. timer.restart();
  86. construct_voronoi(points.begin(), points.end(), &test_output);
  87. periods.push_back(timer.elapsed());
  88. }
  89. }
  90. std::sort(periods.begin(), periods.end());
  91. // Using olympic system to evaluate average time.
  92. double elapsed_time =
  93. std::accumulate(periods.begin() + 2, periods.end() - 2, 0.0);
  94. elapsed_time /= (periods.size() - 4);
  95. std::ofstream bench_file(
  96. BENCHMARK_FILE, std::ios_base::out | std::ios_base::app);
  97. bench_file << std::setiosflags(std::ios::right | std::ios::fixed) << std::setprecision(6);
  98. bench_file << "Static test of " << points.size() << " points: " << elapsed_time << std::endl;
  99. bench_file.close();
  100. }
  101. BOOST_AUTO_TEST_CASE_TEMPLATE(benchmark_test_segments, T, test_types) {
  102. typedef T coordinate_type;
  103. typedef point_data<coordinate_type> point_type;
  104. typedef segment_data<coordinate_type> segment_type;
  105. std::vector<segment_type> segments;
  106. {
  107. std::ifstream input_file(SEGMENT_INPUT_FILE);
  108. int num_segments;
  109. coordinate_type x0, y0, x1, y1;
  110. input_file >> num_segments;
  111. segments.reserve(num_segments);
  112. for (int i = 0; i < num_segments; ++i) {
  113. input_file >> x0 >> y0 >> x1 >> y1;
  114. point_type p0(x0, y0), p1(x1, y1);
  115. segments.push_back(segment_type(p0, p1));
  116. }
  117. input_file.close();
  118. }
  119. std::vector<double> periods;
  120. {
  121. for (int i = 0; i < SEGMENT_RUNS; ++i) {
  122. voronoi_diagram<double> test_output;
  123. timer.restart();
  124. construct_voronoi(segments.begin(), segments.end(), &test_output);
  125. periods.push_back(timer.elapsed());
  126. }
  127. }
  128. std::sort(periods.begin(), periods.end());
  129. // Using olympic system to evaluate average time.
  130. double elapsed_time =
  131. std::accumulate(periods.begin() + 2, periods.end() - 2, 0.0);
  132. elapsed_time /= (periods.size() - 4);
  133. std::ofstream bench_file(
  134. BENCHMARK_FILE, std::ios_base::out | std::ios_base::app);
  135. bench_file << std::setiosflags(std::ios::right | std::ios::fixed) << std::setprecision(6);
  136. bench_file << "Static test of " << segments.size() << " segments: " << elapsed_time << std::endl;
  137. bench_file.close();
  138. }