DGtal  1.3.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 
DGtal::HyperRectDomain::rend
ConstReverseIterator rend() const
Definition: HyperRectDomain.h:236
DGtal::HyperRectDomain< Space >
DGtal::HyperRectDomain::size
Size size() const
Definition: HyperRectDomain.h:565
DGtal::SpaceND::Point
PointVector< dim, Integer > Point
Points in DGtal::SpaceND.
Definition: SpaceND.h:110
TEST_CASE_METHOD
TEST_CASE_METHOD(BenchDomain, "Benchmarking HyperRectDomain iterators using custom implementation")
Definition: benchmarkHyperRectDomain.cpp:89
DGtal::trace
Trace trace
Definition: Common.h:154
DGtal::Dimension
DGtal::uint32_t Dimension
Definition: Common.h:137
dim
unsigned int dim(const Vector &z)
Definition: viewDualSurface.cpp:174
DGtal::SpaceND
Definition: SpaceND.h:95
DGtal::Trace::info
std::ostream & info()
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
tic_timer
auto tic_timer
Timer used in tic and toc.
Definition: benchmarkHyperRectDomain.cpp:47
domain
Domain domain
Definition: testProjection.cpp:88
SECTION
SECTION("Testing constant forward iterators")
Definition: testSimpleRandomAccessRangeFromPoint.cpp:66
DGtal::HyperRectDomain::subRange
ConstSubRange subRange(const std::vector< Dimension > &permutation) const
Definition: HyperRectDomain.h:472
tic
void tic()
Starts timer.
Definition: benchmarkHyperRectDomain.cpp:50
DGtal::HyperRectDomain::rbegin
ConstReverseIterator rbegin() const
Definition: HyperRectDomain.h:210
Point
MyPointD Point
Definition: testClone2.cpp:383
toc
double toc()
Ends timer and return elapsed time.
Definition: benchmarkHyperRectDomain.cpp:56
DGtal::functions::dec::diagonal
DGtal::LinearOperator< Calculus, dim, duality, dim, duality > diagonal(const DGtal::KForm< Calculus, dim, duality > &kform)