DGtal  1.4.beta
benchmarkHyperRectDomain.cpp
Go to the documentation of this file.
1 
32 #include <iostream>
33 #include <numeric>
34 #include <chrono>
35 
36 #include "DGtal/base/Common.h"
37 #include "DGtal/kernel/SpaceND.h"
38 #include "DGtal/kernel/PointVector.h"
39 #include "DGtal/kernel/domains/HyperRectDomain.h"
40 
41 #include "DGtalCatch.h"
42 
43 using namespace DGtal;
44 using namespace std;
45 
47 auto tic_timer = std::chrono::high_resolution_clock::now();
48 
50 void tic()
51 {
52  tic_timer = std::chrono::high_resolution_clock::now();
53 }
54 
56 double toc()
57 {
58  const auto toc_timer = std::chrono::high_resolution_clock::now();
59  const std::chrono::duration<double> time_span = toc_timer - tic_timer;
60  return time_span.count();
61 }
62 
63 // Context for each benchmark
64 struct BenchDomain
65 {
66  static constexpr std::size_t dim = 3;
67  static constexpr signed long long int size = 200;
68  static constexpr std::size_t N = 100;
69 
70  using Space = DGtal::SpaceND<dim>;
71  using Point = Space::Point;
73 
74  BenchDomain()
75  : a(Point::diagonal(0))
76  , b(Point::diagonal(size))
77  , domain(Domain(a, b))
78  , dimensions(Point::dimension)
79  {
80  std::iota(dimensions.begin(), dimensions.end(), Dimension(0));
81  }
82 
83  Point a, b;
84  Domain domain;
85  std::vector<Point::Dimension> dimensions;
86 };
87 
88 
89 TEST_CASE_METHOD( BenchDomain, "Benchmarking HyperRectDomain iterators using custom implementation" )
90 {
91  SECTION("Domain forward traversal")
92  {
93  Point check;
94  double duration;
95 
96  for (std::size_t i = 0; i < N; ++i)
97  {
98  tic();
99  for (auto const& pt : domain)
100  check += pt;
101  duration = toc();
102  }
103 
104  trace.info() << "Domain traversal: " << duration << " s ; " << (domain.size()/duration*1e-9) << " Gpts/s ; check = " << check << std::endl;
105  }
106 
107  SECTION("Domain reverse traversal")
108  {
109  Point check;
110  double duration;
111 
112  for (std::size_t i = 0; i < N; ++i)
113  {
114  tic();
115  for (auto it = domain.rbegin(), it_end = domain.rend(); it != it_end; ++it)
116  check += *it;
117  duration = toc();
118  }
119 
120  trace.info() << "Domain reverse traversal: " << duration << " s ; " << (domain.size()/duration*1e-9) << " Gpts/s ; check = " << check << std::endl;
121  }
122 
123  SECTION("Benchmarking domain traversal using subRange")
124  {
125  Point check;
126  double duration;
127 
128  for (std::size_t i = 0; i < N; ++i)
129  {
130  tic();
131  for (auto const& pt : domain.subRange(dimensions))
132  check += pt;
133  duration = toc();
134  }
135 
136  trace.info() << "Domain traversal using subRange: " << duration << " s ; " << (domain.size()/duration*1e-9) << " Gpts/s ; check = " << check << std::endl;
137  }
138 
139  SECTION("Benchmarking domain reverse traversal using subRange")
140  {
141  Point check;
142  const auto range = domain.subRange(dimensions);
143  double duration;
144 
145  for (std::size_t i = 0; i < N; ++i)
146  {
147  tic();
148  for (auto it = range.rbegin(), it_end = range.rend(); it != it_end; ++it)
149  check += *it;
150  duration = toc();
151  }
152 
153  trace.info() << "Domain reverse traversal using subRange: " << duration << " s ; " << (domain.size()/duration*1e-9) << " Gpts/s ; check = " << check << std::endl;
154  }
155 }
156 
double toc()
Ends timer and return elapsed time.
auto tic_timer
Timer used in tic and toc.
void tic()
Starts timer.
TEST_CASE_METHOD(BenchDomain, "Benchmarking HyperRectDomain iterators using custom implementation")
ConstReverseIterator rbegin() const
ConstSubRange subRange(const std::vector< Dimension > &permutation) const
ConstReverseIterator rend() const
std::ostream & info()
DGtal::LinearOperator< Calculus, dim, duality, dim, duality > diagonal(const DGtal::KForm< Calculus, dim, duality > &kform)
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::uint32_t Dimension
Definition: Common.h:136
Trace trace
Definition: Common.h:153
MyPointD Point
Definition: testClone2.cpp:383
Domain domain
SECTION("Testing constant forward iterators")
unsigned int dim(const Vector &z)