iterator_benchmark.cpp
Go to the documentation of this file.
1 /*
2  * grid_map_iterator_benchmark.hpp
3  *
4  * Created on: Feb 15, 2016
5  * Authors: Christos Zalidis, Péter Fankhauser
6  */
7 
9 #include <chrono>
10 #include <iostream>
11 
12 using namespace std;
13 using namespace std::chrono;
14 using namespace grid_map;
15 
16 #define duration(a) duration_cast<milliseconds>(a).count()
17 typedef high_resolution_clock clk;
18 
22 void runGridMapIteratorVersion1(GridMap& map, const string& layer_from, const string& layer_to)
23 {
24  for (GridMapIterator iterator(map); !iterator.isPastEnd(); ++iterator) {
25  const float value_from = map.at(layer_from, *iterator);
26  float& value_to = map.at(layer_to, *iterator);
27  value_to = value_to > value_from ? value_to : value_from;
28  }
29 }
30 
34 void runGridMapIteratorVersion2(GridMap& map, const string& layer_from, const string& layer_to)
35 {
36  const auto& data_from = map[layer_from];
37  auto& data_to = map[layer_to];
38  for (GridMapIterator iterator(map); !iterator.isPastEnd(); ++iterator) {
39  const Index index(*iterator);
40  const float value_from = data_from(index(0), index(1));
41  float& value_to = data_to(index(0), index(1));
42  value_to = value_to > value_from ? value_to : value_from;
43  }
44 }
45 
49 void runGridMapIteratorVersion3(GridMap& map, const string& layer_from, const string& layer_to)
50 {
51  const auto& data_from = map[layer_from];
52  auto& data_to = map[layer_to];
53  for (GridMapIterator iterator(map); !iterator.isPastEnd(); ++iterator) {
54  const size_t i = iterator.getLinearIndex();
55  const float value_from = data_from(i);
56  float& value_to = data_to(i);
57  value_to = value_to > value_from ? value_to : value_from;
58  }
59 }
60 
65 void runEigenFunction(GridMap& map, const string& layer_from, const string& layer_to)
66 {
67  map[layer_to] = map[layer_to].cwiseMax(map[layer_from]);
68 }
69 
73 void runCustomIndexIteration(GridMap& map, const string& layer_from, const string& layer_to)
74 {
75  const auto& data_from = map[layer_from];
76  auto& data_to = map[layer_to];
77  for (size_t j = 0; j < data_to.cols(); ++j) {
78  for (size_t i = 0; i < data_to.rows(); ++i) {
79  const float value_from = data_from(i, j);
80  float& value_to = data_to(i, j);
81  value_to = value_to > value_from ? value_to : value_from;
82  }
83  }
84 }
85 
89 void runCustomLinearIndexIteration(GridMap& map, const string& layer_from, const string& layer_to)
90 {
91  const auto& data_from = map[layer_from];
92  auto& data_to = map[layer_to];
93  for (size_t i = 0; i < data_to.size(); ++i) {
94  data_to(i) = data_to(i) > data_from(i) ? data_to(i) : data_from(i);
95  }
96 }
97 
98 int main(int argc, char* argv[])
99 {
100  GridMap map;
101  map.setGeometry(Length(20.0, 20.0), 0.004, Position(0.0, 0.0));
102  map.add("random");
103  map["random"].setRandom();
104  map.add("layer1", 0.0);
105  map.add("layer2", 0.0);
106  map.add("layer3", 0.0);
107  map.add("layer4", 0.0);
108  map.add("layer5", 0.0);
109  map.add("layer6", 0.0);
110 
111  cout << "Results for iteration over " << map.getSize()(0) << " x " << map.getSize()(1) << " (" << map.getSize().prod() << ") grid cells." << endl;
112  cout << "=========================================" << endl;
113 
114  clk::time_point t1 = clk::now();
115  runGridMapIteratorVersion1(map, "random", "layer1");
116  clk::time_point t2 = clk::now();
117  cout << "Duration grid map iterator (convenient use): " << duration(t2 - t1) << " ms" << endl;
118 
119  t1 = clk::now();
120  runGridMapIteratorVersion2(map, "random", "layer2");
121  t2 = clk::now();
122  cout << "Duration grid map iterator (direct access to data layers): " << duration(t2 - t1) << " ms" << endl;
123 
124  t1 = clk::now();
125  runGridMapIteratorVersion3(map, "random", "layer3");
126  t2 = clk::now();
127  cout << "Duration grid map iterator (linear index): " << duration(t2 - t1) << " ms" << endl;
128 
129  t1 = clk::now();
130  runEigenFunction(map, "random", "layer4");
131  t2 = clk::now();
132  cout << "Duration Eigen function: " << duration(t2 - t1) << " ms" << endl;
133 
134  t1 = clk::now();
135  runCustomIndexIteration(map, "random", "layer5");
136  t2 = clk::now();
137  cout << "Duration custom index iteration: " << duration(t2 - t1) << " ms" << endl;
138 
139  t1 = clk::now();
140  runCustomLinearIndexIteration(map, "random", "layer6");
141  t2 = clk::now();
142  cout << "Duration custom linear index iteration: " << duration(t2 - t1) << " ms" << endl;
143 
144  return 0;
145 }
Eigen::Array2i Index
void setGeometry(const Length &length, const double resolution, const Position &position=Position::Zero())
void runCustomIndexIteration(GridMap &map, const string &layer_from, const string &layer_to)
void runEigenFunction(GridMap &map, const string &layer_from, const string &layer_to)
#define duration(a)
void runGridMapIteratorVersion1(GridMap &map, const string &layer_from, const string &layer_to)
Eigen::Vector2d Position
void runGridMapIteratorVersion2(GridMap &map, const string &layer_from, const string &layer_to)
float & at(const std::string &layer, const Index &index)
void runGridMapIteratorVersion3(GridMap &map, const string &layer_from, const string &layer_to)
high_resolution_clock clk
void add(const std::string &layer, const double value=NAN)
int main(int argc, char *argv[])
void runCustomLinearIndexIteration(GridMap &map, const string &layer_from, const string &layer_to)
Eigen::Array2d Length
const Size & getSize() const


grid_map_demos
Author(s): Péter Fankhauser
autogenerated on Tue Jun 25 2019 20:02:37