1 // Boost.Polygon library voronoi_benchmark.cpp file
3 // Copyright Andrii Sydorchuk 2010-2012.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // See http://www.boost.org for updates, documentation, and revision history.
18 #define BOOST_TEST_MODULE benchmark_test
19 #include <boost/mpl/list.hpp>
20 #include <boost/random/mersenne_twister.hpp>
21 #include <boost/test/test_case_template.hpp>
22 #include <boost/timer.hpp>
24 #include <boost/polygon/polygon.hpp>
25 #include <boost/polygon/voronoi.hpp>
26 using boost::polygon::point_data;
27 using boost::polygon::segment_data;
28 using boost::polygon::voronoi_diagram;
30 typedef boost::mpl::list<int> test_types;
31 const char *BENCHMARK_FILE = "voronoi_benchmark.txt";
32 const char *POINT_INPUT_FILE = "input_data/voronoi_point.txt";
33 const char *SEGMENT_INPUT_FILE = "input_data/voronoi_segment.txt";
34 const int POINT_RUNS = 10;
35 const int SEGMENT_RUNS = 10;
37 boost::mt19937 gen(static_cast<unsigned int>(time(NULL)));
40 BOOST_AUTO_TEST_CASE_TEMPLATE(benchmark_test_random, T, test_types) {
41 typedef T coordinate_type;
42 typedef point_data<coordinate_type> point_type;
43 voronoi_diagram<double> test_output;
45 std::ofstream bench_file(BENCHMARK_FILE, std::ios_base::out | std::ios_base::app);
46 bench_file << "Voronoi Benchmark Test (time in seconds):" << std::endl;
47 bench_file << "| Number of points | Number of tests | Time per one test |" << std::endl;
48 bench_file << std::setiosflags(std::ios::right | std::ios::fixed) << std::setprecision(6);
49 int max_points = 100000;
50 std::vector<point_type> points;
52 for (int num_points = 10; num_points <= max_points; num_points *= 10) {
53 points.resize(num_points);
55 int num_tests = max_points / num_points;
56 for (int cur = 0; cur < num_tests; cur++) {
58 for (int cur_point = 0; cur_point < num_points; cur_point++) {
59 x = static_cast<coordinate_type>(gen());
60 y = static_cast<coordinate_type>(gen());
61 points[cur_point] = point_type(x, y);
63 construct_voronoi(points.begin(), points.end(), &test_output);
65 double elapsed_time = timer.elapsed();
66 double time_per_test = elapsed_time / num_tests;
68 bench_file << "| " << std::setw(16) << num_points << " ";
69 bench_file << "| " << std::setw(15) << num_tests << " ";
70 bench_file << "| " << std::setw(17) << time_per_test << " ";
71 bench_file << "| " << std::endl;
76 BOOST_AUTO_TEST_CASE_TEMPLATE(benchmark_test_points, T, test_types) {
77 typedef T coordinate_type;
78 typedef point_data<coordinate_type> point_type;
80 std::vector<point_type> points;
82 std::ifstream input_file(POINT_INPUT_FILE);
85 input_file >> num_points;
86 points.reserve(num_points);
87 for (int i = 0; i < num_points; ++i) {
89 points.push_back(point_type(x, y));
94 std::vector<double> periods;
96 for (int i = 0; i < POINT_RUNS; ++i) {
97 voronoi_diagram<double> test_output;
99 construct_voronoi(points.begin(), points.end(), &test_output);
100 periods.push_back(timer.elapsed());
103 std::sort(periods.begin(), periods.end());
104 // Using olympic system to evaluate average time.
105 double elapsed_time =
106 std::accumulate(periods.begin() + 2, periods.end() - 2, 0.0);
107 elapsed_time /= (periods.size() - 4);
109 std::ofstream bench_file(
110 BENCHMARK_FILE, std::ios_base::out | std::ios_base::app);
111 bench_file << std::setiosflags(std::ios::right | std::ios::fixed) << std::setprecision(6);
112 bench_file << "Static test of " << points.size() << " points: " << elapsed_time << std::endl;
116 BOOST_AUTO_TEST_CASE_TEMPLATE(benchmark_test_segments, T, test_types) {
117 typedef T coordinate_type;
118 typedef point_data<coordinate_type> point_type;
119 typedef segment_data<coordinate_type> segment_type;
121 std::vector<segment_type> segments;
123 std::ifstream input_file(SEGMENT_INPUT_FILE);
125 coordinate_type x0, y0, x1, y1;
126 input_file >> num_segments;
127 segments.reserve(num_segments);
128 for (int i = 0; i < num_segments; ++i) {
129 input_file >> x0 >> y0 >> x1 >> y1;
130 point_type p0(x0, y0), p1(x1, y1);
131 segments.push_back(segment_type(p0, p1));
136 std::vector<double> periods;
138 for (int i = 0; i < SEGMENT_RUNS; ++i) {
139 voronoi_diagram<double> test_output;
141 construct_voronoi(segments.begin(), segments.end(), &test_output);
142 periods.push_back(timer.elapsed());
145 std::sort(periods.begin(), periods.end());
146 // Using olympic system to evaluate average time.
147 double elapsed_time =
148 std::accumulate(periods.begin() + 2, periods.end() - 2, 0.0);
149 elapsed_time /= (periods.size() - 4);
151 std::ofstream bench_file(
152 BENCHMARK_FILE, std::ios_base::out | std::ios_base::app);
153 bench_file << std::setiosflags(std::ios::right | std::ios::fixed) << std::setprecision(6);
154 bench_file << "Static test of " << segments.size() << " segments: " << elapsed_time << std::endl;