DGtal  1.5.beta
benchmarkImageContainer.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
32 #include <benchmark/benchmark.h>
33 #include "DGtal/base/Common.h"
34 
35 #include "DGtal/base/Common.h"
36 #include "DGtal/kernel/SpaceND.h"
37 #include "DGtal/kernel/domains/HyperRectDomain.h"
38 #include "DGtal/images/ImageSelector.h"
39 
40 #include "DGtal/helpers/StdDefs.h"
41 #include <map>
42 #include <string>
43 
45 
46 using namespace std;
47 using namespace DGtal;
49 
53 
54 template<typename Q>
55 static void BM_Constructor(benchmark::State& state)
56 {
57  while (state.KeepRunning())
58  {
59  state.PauseTiming();
60  typename Q::Domain dom(typename Q::Point().diagonal(0),
61  typename Q::Point().diagonal(state.range(0)));
62  state.ResumeTiming();
63  Q image( dom );
64  }
65 }
67 BENCHMARK_TEMPLATE(BM_Constructor, ImageMap2)->Range(1<<3 , 1 << 16);
68 BENCHMARK_TEMPLATE(BM_Constructor, ImageHash2)->Range(1<<3 , 1 << 16);
69 
70 template<typename Point>
71 std::set<Point> ConstructRandomSet(unsigned int size, unsigned int maxWidth) {
72  std::set<Point> s;
73  Point p;
74  for (unsigned int i = 0; i < size; ++i)
75  {
76  for(unsigned int j=0; j < Point::dimension; j++)
77  p[j] = rand() % maxWidth;
78  s.insert( p );
79  }
80  return s;
81 }
82 
83 
84 template<typename Q>
85 static void BM_SetValue(benchmark::State& state)
86 {
87  std::set<typename Q::Point> data = ConstructRandomSet<typename Q::Point>(state.range(0),state.range(0));
88 
89  int64_t cpt=0;
90  while (state.KeepRunning())
91  {
92  state.PauseTiming();
93  typename Q::Domain dom(typename Q::Point().diagonal(0),
94  typename Q::Point().diagonal(state.range(0)));
95  Q image( dom );
96  for(typename std::set<typename Q::Point>::const_iterator it = data.begin(), itend=data.end();
97  it != itend; ++it)
98  {
99  state.ResumeTiming();
100  image.setValue( *it , 42);
101  state.PauseTiming();
102  cpt++;
103  }
104  }
105  // const int64_t items_processed =
106  // static_cast<int64_t>(state.iterations())*state.range(0);
107  state.SetItemsProcessed(cpt);
108 }
109 BENCHMARK_TEMPLATE(BM_SetValue, ImageVector2)->Range(1<<3 , 1 << 10);
110 BENCHMARK_TEMPLATE(BM_SetValue, ImageMap2)->Range(1<<3 , 1 << 16);
111 BENCHMARK_TEMPLATE(BM_SetValue, ImageHash2)->Range(1<<3 , 1 << 10);
112 
113 template<typename Q>
114 static void BM_RangeScan(benchmark::State& state)
115 {
116  std::set<typename Q::Point> data = ConstructRandomSet<typename Q::Point>(state.range(0),state.range(0));
117  int sum=0;
118  while (state.KeepRunning())
119  {
120  state.PauseTiming();
121  typename Q::Domain dom(typename Q::Point().diagonal(0),
122  typename Q::Point().diagonal(state.range(0)));
123  Q image( dom );
124  for(typename std::set<typename Q::Point>::const_iterator it = data.begin(), itend=data.end();
125  it != itend; ++it)
126  image.setValue( *it , 42);
127  state.ResumeTiming();
128  for(typename Q::Range::ConstIterator it = image.range().begin(), itend=image.range().end();
129  it != itend; ++it)
130  benchmark::DoNotOptimize(sum += *it); //to prevent compiler optimization
131  }
132  std::stringstream ss;
133  ss << sum;
134  state.SetLabel(ss.str());
135 }
137 BENCHMARK_TEMPLATE(BM_RangeScan, ImageMap2)->Range(1<<3 , 1 << 10);
138 
139 template<typename Q>
140 static void BM_DomainScan(benchmark::State& state)
141 {
142  std::set<typename Q::Point> data = ConstructRandomSet<typename Q::Point>(state.range(0),state.range(0));
143  int sum=0;
144  while (state.KeepRunning())
145  {
146  state.PauseTiming();
147  typename Q::Domain dom(typename Q::Point().diagonal(0),
148  typename Q::Point().diagonal(state.range(0)));
149  Q image( dom );
150  for(typename std::set<typename Q::Point>::const_iterator it = data.begin(), itend=data.end();
151  it != itend; ++it)
152  image.setValue( *it , 42);
153  state.ResumeTiming();
154  for(typename Q::Domain::ConstIterator it = image.domain().begin(), itend=image.domain().end();
155  it != itend; ++it)
156  benchmark::DoNotOptimize( sum ++ );
157  }
158  std::stringstream ss;
159  ss << sum;
160  state.SetLabel(ss.str());
161 }
163 BENCHMARK_TEMPLATE(BM_DomainScan, ImageMap2)->Range(1<<3 , 1 << 10);
164 
165 
166 
167 
169 // Standard services - public :
170 
171 int main( int argc, char **argv )
172 {
173  benchmark::Initialize(&argc, argv);
174 
175  benchmark::RunSpecifiedBenchmarks();
176  return 0;
177 }
178 // //
int main(int argc, char **argv)
static void BM_SetValue(benchmark::State &state)
static void BM_Constructor(benchmark::State &state)
BENCHMARK_TEMPLATE(BM_Constructor, ImageVector2) -> Range(1<< 3, 1<< 10)
std::set< Point > ConstructRandomSet(unsigned int size, unsigned int maxWidth)
DGtal::ImageContainerBySTLVector< Z2i::Domain, DGtal::int32_t > ImageVector2
DGtal::experimental::ImageContainerByHashTree< Z2i::Domain, DGtal::int32_t > ImageHash2
DGtal::ImageContainerBySTLMap< Z2i::Domain, DGtal::int32_t > ImageMap2
static void BM_RangeScan(benchmark::State &state)
static void BM_DomainScan(benchmark::State &state)
Model of CImageContainer implementing the association key<->Value using a hash tree....
MyDigitalSurface::ConstIterator ConstIterator
DGtal is the top-level namespace which contains all DGtal functions and types.
boost::int64_t int64_t
signed 94-bit integer.
Definition: BasicTypes.h:74
MyPointD Point
Definition: testClone2.cpp:383
Image image(domain)
HyperRectDomain< Space > Domain