iterator_bench.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "simple/geom/iterator.hpp"
  2. #include "simple/geom/algorithm.hpp"
  3. #include <cassert>
  4. #include <memory>
  5. #include <chrono>
  6. #include <iostream>
  7. // #include <boost/gil.hpp>
  8. using namespace simple::geom;
  9. using namespace std::chrono;
  10. #undef assert
  11. #define assert(x) if(not (x)) std::puts("aaaa!");
  12. template <unsigned width, unsigned height, unsigned step>
  13. void BasicReadWrite()
  14. {
  15. constexpr vector size{width,height};
  16. constexpr unsigned pitch = size.x() + step;
  17. auto image_ptr = std::make_unique<std::array<unsigned, pitch * size.y()>>();
  18. auto& image = *image_ptr;
  19. loop(size, [&, position = unsigned()] (auto i) mutable
  20. {
  21. image[i.y() * pitch + i.x()] = position;
  22. ++position;
  23. });
  24. std::cout << "generic" << '\n';
  25. {
  26. auto now = high_resolution_clock::now();
  27. iterator begin{image.begin(), 0u};
  28. iterator end{image.end()-step, size.x()};
  29. unsigned i = 0;
  30. while(true)
  31. {
  32. auto done = begin == end;
  33. if(done) break;
  34. if(done.first_false != 0)
  35. begin.next(step,done);
  36. assert(*begin == i);
  37. ++begin;
  38. ++i;
  39. }
  40. assert(i == size.x() * size.y());
  41. std::cout << (high_resolution_clock::now() - now).count() << '\n';
  42. }
  43. std::cout << "generic vectirator" << '\n';
  44. {
  45. auto now = high_resolution_clock::now();
  46. vectirator begin{image.begin(), {0u, 0u}};
  47. vectirator end{image.begin(), {size.x(), size.y()*pitch - step}};
  48. unsigned i = 0;
  49. while(true)
  50. {
  51. auto done = begin == end;
  52. if(done) break;
  53. if(to_disjunction(done))
  54. begin.next({1u, step}, done);
  55. assert(*begin == i);
  56. ++begin;
  57. ++i;
  58. }
  59. assert(i == size.x() * size.y());
  60. std::cout << (high_resolution_clock::now() - now).count() << '\n';
  61. }
  62. // std::cout << "generic nested" << '\n';
  63. // {
  64. // auto now = high_resolution_clock::now();
  65. // iterator begin{image.begin(), 0u};
  66. // iterator end{image.end()-step, size.x()};
  67. // unsigned i = 0;
  68. // while(true)
  69. // {
  70. // auto done = begin == end;
  71. // if(done) break;
  72. // // if(to_disjunction(done))
  73. // if(done.first_false != 0)
  74. // begin.next(step,done);
  75. // while(begin != end)
  76. // {
  77. // assert(*begin == i);
  78. // ++begin;
  79. // ++i;
  80. // };
  81. // }
  82. // assert(i == size.x() * size.y());
  83. // std::cout << (high_resolution_clock::now() - now).count() << '\n';
  84. // }
  85. // std::cout << "indexing" << '\n';
  86. // {
  87. // auto now = high_resolution_clock::now();
  88. // unsigned j = 0;
  89. // loop(size, [&](auto i)
  90. // {
  91. // auto index = i.y() * pitch + i.x();
  92. // assert(image[index] == j);
  93. // j++;
  94. // });
  95. // assert(j == size.x() * size.y());
  96. // std::cout << (high_resolution_clock::now() - now).count() << '\n';
  97. // }
  98. // std::cout << "boost gil index" << '\n';
  99. // {
  100. // auto now = high_resolution_clock::now();
  101. // auto view = boost::gil::interleaved_view(size.x(), size.y(), image.begin(), pitch * sizeof(decltype(*image.begin())));
  102. // unsigned i = 0;
  103. // for(int y = 0; y < view.height(); ++y)
  104. // {
  105. // for(int x = 0; x < view.width(); ++x)
  106. // {
  107. // assert(i == view(x,y));
  108. // ++i;
  109. // }
  110. // }
  111. // assert(i == size.x() * size.y());
  112. // std::cout << (high_resolution_clock::now() - now).count() << '\n';
  113. // }
  114. // std::cout << "boost gil iterator" << '\n';
  115. // {
  116. // auto now = high_resolution_clock::now();
  117. // auto view = boost::gil::interleaved_view(size.x(), size.y(), image.begin(), pitch * sizeof(decltype(*image.begin())));
  118. // unsigned i = 0;
  119. // for(int y = 0; y < view.height(); ++y)
  120. // {
  121. // auto it = view.row_begin(y);
  122. // for(int x = 0; x < view.width(); ++x)
  123. // {
  124. // assert(i == *it);
  125. // ++it;
  126. // ++i;
  127. // }
  128. // }
  129. // assert(i == size.x() * size.y());
  130. // std::cout << (high_resolution_clock::now() - now).count() << '\n';
  131. // }
  132. // std::cout << "hand rolled" << '\n';
  133. // {
  134. // auto now = high_resolution_clock::now();
  135. // auto begin = image.begin();
  136. // unsigned i = 0;
  137. // for(unsigned y = 0; y != size.y(); ++y)
  138. // {
  139. // std::for_each(begin, begin + size.x(), [&i](auto x)
  140. // {
  141. // assert(x == i);
  142. // ++i;
  143. // });
  144. // begin += pitch;
  145. // }
  146. // assert(i == size.x() * size.y());
  147. // std::cout << (high_resolution_clock::now() - now).count() << '\n';
  148. // }
  149. // std::cout << "hand rolled index" << '\n';
  150. // {
  151. // auto now = high_resolution_clock::now();
  152. // unsigned i = 0;
  153. // for(int y = 0; y != (int)size.y(); ++y)
  154. // {
  155. // for(int x = 0; x != (int)size.x(); ++x)
  156. // {
  157. // auto index = y * pitch + x;
  158. // assert(i == image[index]);
  159. // ++i;
  160. // }
  161. // }
  162. // assert(i == size.x() * size.y());
  163. // std::cout << (high_resolution_clock::now() - now).count() << '\n';
  164. // }
  165. std::cout << "--------------" << '\n';
  166. }
  167. int main()
  168. {
  169. // return 0;
  170. BasicReadWrite<1,2,3>();
  171. BasicReadWrite<1,1,1>();
  172. BasicReadWrite<1,2,0>();
  173. BasicReadWrite<1,1,0>();
  174. // TODO: figure out what's wrong with these and if needs fixing
  175. // BasicReadWrite<1,0,3>();
  176. // BasicReadWrite<1,0,0>();
  177. BasicReadWrite<0,0,0>();
  178. BasicReadWrite<1000,1000,200>();
  179. BasicReadWrite<2000,2000,4000>();
  180. BasicReadWrite<1000,1000,0>();
  181. BasicReadWrite<2000,2000,0>();
  182. // BasicReadWrite<20000,20000,0>();
  183. }