benchmark.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Boost.Geometry Index
  2. // Additional tests
  3. // Copyright (c) 2011-2013 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 <boost/geometry.hpp>
  9. #include <boost/geometry/index/rtree.hpp>
  10. #include <boost/chrono.hpp>
  11. #include <boost/foreach.hpp>
  12. #include <boost/random.hpp>
  13. int main()
  14. {
  15. namespace bg = boost::geometry;
  16. namespace bgi = bg::index;
  17. typedef boost::chrono::thread_clock clock_t;
  18. typedef boost::chrono::duration<float> dur_t;
  19. size_t values_count = 1000000;
  20. size_t queries_count = 100000;
  21. size_t nearest_queries_count = 10000;
  22. unsigned neighbours_count = 10;
  23. std::vector< std::pair<float, float> > coords;
  24. //randomize values
  25. {
  26. boost::mt19937 rng;
  27. //rng.seed(static_cast<unsigned int>(std::time(0)));
  28. float max_val = static_cast<float>(values_count / 2);
  29. boost::uniform_real<float> range(-max_val, max_val);
  30. boost::variate_generator<boost::mt19937&, boost::uniform_real<float> > rnd(rng, range);
  31. coords.reserve(values_count);
  32. std::cout << "randomizing data\n";
  33. for ( size_t i = 0 ; i < values_count ; ++i )
  34. {
  35. coords.push_back(std::make_pair(rnd(), rnd()));
  36. }
  37. std::cout << "randomized\n";
  38. }
  39. typedef bg::model::point<double, 2, bg::cs::cartesian> P;
  40. typedef bg::model::box<P> B;
  41. typedef bgi::rtree<B, bgi::linear<16, 4> > RT;
  42. //typedef bgi::rtree<B, bgi::quadratic<8, 3> > RT;
  43. //typedef bgi::rtree<B, bgi::rstar<8, 3> > RT;
  44. std::cout << "sizeof rtree: " << sizeof(RT) << std::endl;
  45. for (;;)
  46. {
  47. RT t;
  48. // inserting test
  49. {
  50. clock_t::time_point start = clock_t::now();
  51. for (size_t i = 0 ; i < values_count ; ++i )
  52. {
  53. float x = coords[i].first;
  54. float y = coords[i].second;
  55. B b(P(x - 0.5f, y - 0.5f), P(x + 0.5f, y + 0.5f));
  56. t.insert(b);
  57. }
  58. dur_t time = clock_t::now() - start;
  59. std::cout << time << " - insert " << values_count << '\n';
  60. }
  61. std::vector<B> result;
  62. result.reserve(100);
  63. B result_one;
  64. {
  65. clock_t::time_point start = clock_t::now();
  66. size_t temp = 0;
  67. for (size_t i = 0 ; i < queries_count ; ++i )
  68. {
  69. float x = coords[i].first;
  70. float y = coords[i].second;
  71. result.clear();
  72. t.query(bgi::intersects(B(P(x - 10, y - 10), P(x + 10, y + 10))), std::back_inserter(result));
  73. temp += result.size();
  74. }
  75. dur_t time = clock_t::now() - start;
  76. std::cout << time << " - query(B) " << queries_count << " found " << temp << '\n';
  77. }
  78. {
  79. clock_t::time_point start = clock_t::now();
  80. size_t temp = 0;
  81. for (size_t i = 0 ; i < queries_count / 2 ; ++i )
  82. {
  83. float x1 = coords[i].first;
  84. float y1 = coords[i].second;
  85. float x2 = coords[i+1].first;
  86. float y2 = coords[i+1].second;
  87. float x3 = coords[i+2].first;
  88. float y3 = coords[i+2].second;
  89. result.clear();
  90. t.query(
  91. bgi::intersects(B(P(x1 - 10, y1 - 10), P(x1 + 10, y1 + 10)))
  92. &&
  93. !bgi::within(B(P(x2 - 10, y2 - 10), P(x2 + 10, y2 + 10)))
  94. &&
  95. !bgi::overlaps(B(P(x3 - 10, y3 - 10), P(x3 + 10, y3 + 10)))
  96. ,
  97. std::back_inserter(result)
  98. );
  99. temp += result.size();
  100. }
  101. dur_t time = clock_t::now() - start;
  102. std::cout << time << " - query(i && !w && !o) " << queries_count << " found " << temp << '\n';
  103. }
  104. result.clear();
  105. {
  106. clock_t::time_point start = clock_t::now();
  107. size_t temp = 0;
  108. for (size_t i = 0 ; i < nearest_queries_count ; ++i )
  109. {
  110. float x = coords[i].first + 100;
  111. float y = coords[i].second + 100;
  112. result.clear();
  113. temp += t.query(bgi::nearest(P(x, y), neighbours_count), std::back_inserter(result));
  114. }
  115. dur_t time = clock_t::now() - start;
  116. std::cout << time << " - query(nearest(P, " << neighbours_count << ")) " << nearest_queries_count << " found " << temp << '\n';
  117. }
  118. {
  119. clock_t::time_point start = clock_t::now();
  120. for (size_t i = 0 ; i < values_count / 10 ; ++i )
  121. {
  122. float x = coords[i].first;
  123. float y = coords[i].second;
  124. B b(P(x - 0.5f, y - 0.5f), P(x + 0.5f, y + 0.5f));
  125. t.remove(b);
  126. }
  127. dur_t time = clock_t::now() - start;
  128. std::cout << time << " - remove " << values_count / 10 << '\n';
  129. }
  130. std::cout << "------------------------------------------------\n";
  131. }
  132. return 0;
  133. }