benchmark_insert.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Boost.Geometry Index
  2. // Additional tests
  3. // Copyright (c) 2011-2015 Adam Wulkiewicz, Lodz, Poland.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <iostream>
  8. #include <vector>
  9. #include <algorithm>
  10. #include <boost/chrono.hpp>
  11. #include <boost/foreach.hpp>
  12. #include <boost/random.hpp>
  13. #include <boost/geometry.hpp>
  14. #include <boost/geometry/index/rtree.hpp>
  15. #include <boost/geometry/geometries/geometries.hpp>
  16. #include <boost/geometry/index/detail/rtree/utilities/are_boxes_ok.hpp>
  17. #include <boost/geometry/index/detail/rtree/utilities/are_counts_ok.hpp>
  18. #include <boost/geometry/index/detail/rtree/utilities/are_levels_ok.hpp>
  19. namespace bg = boost::geometry;
  20. namespace bgi = bg::index;
  21. typedef bg::model::point<double, 2, bg::cs::cartesian> P;
  22. typedef bg::model::box<P> B;
  23. typedef bg::model::segment<P> S;
  24. typedef P V;
  25. //typedef B V;
  26. //typedef S V;
  27. template <typename V>
  28. struct generate_value {};
  29. template <>
  30. struct generate_value<B>
  31. {
  32. static inline B apply(float x, float y) { return B(P(x - 0.5f, y - 0.5f), P(x + 0.5f, y + 0.5f)); }
  33. };
  34. template <>
  35. struct generate_value<S>
  36. {
  37. static inline S apply(float x, float y) { return S(P(x - 0.5f, y - 0.5f), P(x + 0.5f, y + 0.5f)); }
  38. };
  39. template <>
  40. struct generate_value<P>
  41. {
  42. static inline P apply(float x, float y) { return P(x, y); }
  43. };
  44. template <typename RT>
  45. void test_queries(RT const& t, std::vector< std::pair<float, float> > const& coords, size_t queries_count)
  46. {
  47. typedef boost::chrono::thread_clock clock_t;
  48. typedef boost::chrono::duration<float> dur_t;
  49. std::vector<V> result;
  50. result.reserve(100);
  51. clock_t::time_point start = clock_t::now();
  52. size_t temp = 0;
  53. for (size_t i = 0 ; i < queries_count ; ++i )
  54. {
  55. float x = coords[i].first;
  56. float y = coords[i].second;
  57. result.clear();
  58. t.query(bgi::intersects(B(P(x - 10, y - 10), P(x + 10, y + 10))), std::back_inserter(result));
  59. temp += result.size();
  60. }
  61. dur_t time = clock_t::now() - start;
  62. std::cout << time.count() << " " << temp << '\n';
  63. }
  64. //#define BOOST_GEOMETRY_INDEX_BENCHMARK_DEBUG
  65. int main()
  66. {
  67. //typedef bgi::rtree<V, bgi::linear<4, 2> > RT;
  68. //typedef bgi::rtree<V, bgi::linear<16, 4> > RT;
  69. //typedef bgi::rtree<V, bgi::quadratic<4, 2> > RT;
  70. typedef bgi::rtree<V, bgi::rstar<8, 2> > RT;
  71. typedef boost::chrono::thread_clock clock_t;
  72. typedef boost::chrono::duration<float> dur_t;
  73. #ifndef BOOST_GEOMETRY_INDEX_BENCHMARK_DEBUG
  74. size_t values_count = 1000000;
  75. size_t queries_count = 100000;
  76. size_t nearest_queries_count = 20000;
  77. unsigned neighbours_count = 10;
  78. size_t max_range_inserts = 10;
  79. #else
  80. size_t values_count = 10000;
  81. size_t queries_count = 1000;
  82. size_t nearest_queries_count = 100;
  83. unsigned neighbours_count = 10;
  84. size_t max_range_inserts = 10;
  85. #endif
  86. float max_val = static_cast<float>(values_count / 2);
  87. std::vector< std::pair<float, float> > coords;
  88. std::vector<V> values;
  89. //randomize values
  90. {
  91. boost::mt19937 rng;
  92. //rng.seed(static_cast<unsigned int>(std::time(0)));
  93. boost::uniform_real<float> range(-max_val, max_val);
  94. boost::variate_generator<boost::mt19937&, boost::uniform_real<float> > rnd(rng, range);
  95. coords.reserve(values_count);
  96. std::cout << "randomizing data\n";
  97. for ( size_t i = 0 ; i < values_count ; ++i )
  98. {
  99. float x = rnd();
  100. float y = rnd();
  101. coords.push_back(std::make_pair(x, y));
  102. values.push_back(generate_value<V>::apply(x, y));
  103. }
  104. std::cout << "randomized\n";
  105. }
  106. for (;;)
  107. {
  108. // packing test
  109. {
  110. clock_t::time_point start = clock_t::now();
  111. RT t(values.begin(), values.end());
  112. BOOST_ASSERT(bgi::detail::rtree::utilities::are_boxes_ok(t));
  113. BOOST_ASSERT(bgi::detail::rtree::utilities::are_counts_ok(t));
  114. BOOST_ASSERT(bgi::detail::rtree::utilities::are_levels_ok(t));
  115. dur_t time = clock_t::now() - start;
  116. std::cout << "pack(" << values_count << ") - " << time.count() << ", ";
  117. test_queries(t, coords, queries_count);
  118. }
  119. {
  120. size_t n_per_max = values_count / max_range_inserts;
  121. for ( size_t j = 0 ; j < max_range_inserts ; ++j )
  122. {
  123. clock_t::time_point start = clock_t::now();
  124. RT t;
  125. // perform j range-inserts
  126. for ( size_t i = 0 ; i < j ; ++i )
  127. {
  128. t.insert(values.begin() + n_per_max * i,
  129. values.begin() + (std::min)(n_per_max * (i + 1), values_count));
  130. }
  131. if ( !t.empty() )
  132. {
  133. BOOST_ASSERT(bgi::detail::rtree::utilities::are_boxes_ok(t));
  134. BOOST_ASSERT(bgi::detail::rtree::utilities::are_counts_ok(t));
  135. BOOST_ASSERT(bgi::detail::rtree::utilities::are_levels_ok(t));
  136. }
  137. // perform n-n/max_inserts*j inserts
  138. size_t inserted_count = (std::min)(n_per_max*j, values_count);
  139. for ( size_t i = inserted_count ; i < values_count ; ++i )
  140. {
  141. t.insert(values[i]);
  142. }
  143. if ( !t.empty() )
  144. {
  145. BOOST_ASSERT(bgi::detail::rtree::utilities::are_boxes_ok(t));
  146. BOOST_ASSERT(bgi::detail::rtree::utilities::are_counts_ok(t));
  147. BOOST_ASSERT(bgi::detail::rtree::utilities::are_levels_ok(t));
  148. }
  149. dur_t time = clock_t::now() - start;
  150. std::cout << j << "*insert(N/" << max_range_inserts << ")+insert(" << (values_count - inserted_count) << ") - " << time.count() << ", ";
  151. test_queries(t, coords, queries_count);
  152. }
  153. }
  154. std::cout << "------------------------------------------------\n";
  155. }
  156. return 0;
  157. }