-
Notifications
You must be signed in to change notification settings - Fork 828
/
Copy pathiterator_benchmark.cpp
145 lines (127 loc) · 4.43 KB
/
iterator_benchmark.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* grid_map_iterator_benchmark.hpp
*
* Created on: Feb 15, 2016
* Authors: Christos Zalidis, Péter Fankhauser
*/
#include <grid_map_core/grid_map_core.hpp>
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;
using namespace grid_map;
#define duration(a) duration_cast<milliseconds>(a).count()
typedef high_resolution_clock clk;
/*!
* Convenient use of iterator.
*/
void runGridMapIteratorVersion1(GridMap& map, const string& layer_from, const string& layer_to)
{
for (GridMapIterator iterator(map); !iterator.isPastEnd(); ++iterator) {
const float value_from = map.at(layer_from, *iterator);
float& value_to = map.at(layer_to, *iterator);
value_to = value_to > value_from ? value_to : value_from;
}
}
/*!
* Improved efficiency by storing direct access to data layers.
*/
void runGridMapIteratorVersion2(GridMap& map, const string& layer_from, const string& layer_to)
{
const auto& data_from = map[layer_from];
auto& data_to = map[layer_to];
for (GridMapIterator iterator(map); !iterator.isPastEnd(); ++iterator) {
const Index index(*iterator);
const float value_from = data_from(index(0), index(1));
float& value_to = data_to(index(0), index(1));
value_to = value_to > value_from ? value_to : value_from;
}
}
/*!
* Improved efficiency by using linear index.
*/
void runGridMapIteratorVersion3(GridMap& map, const string& layer_from, const string& layer_to)
{
const auto& data_from = map[layer_from];
auto& data_to = map[layer_to];
for (GridMapIterator iterator(map); !iterator.isPastEnd(); ++iterator) {
const size_t i = iterator.getLinearIndex();
const float value_from = data_from(i);
float& value_to = data_to(i);
value_to = value_to > value_from ? value_to : value_from;
}
}
/*!
* Whenever possible, make use of the Eigen methods for maximum efficiency
* and readability.
*/
void runEigenFunction(GridMap& map, const string& layer_from, const string& layer_to)
{
map[layer_to] = map[layer_to].cwiseMax(map[layer_from]);
}
/*!
* For comparison.
*/
void runCustomIndexIteration(GridMap& map, const string& layer_from, const string& layer_to)
{
const auto& data_from = map[layer_from];
auto& data_to = map[layer_to];
for (Eigen::Index j{0}; j < data_to.cols(); ++j) {
for (Eigen::Index i{0}; i < data_to.rows(); ++i) {
const float value_from = data_from(i, j);
float& value_to = data_to(i, j);
value_to = value_to > value_from ? value_to : value_from;
}
}
}
/*!
* For comparison.
*/
void runCustomLinearIndexIteration(GridMap& map, const string& layer_from, const string& layer_to)
{
const auto& data_from = map[layer_from];
auto& data_to = map[layer_to];
for (Eigen::Index i = 0; i < data_to.size(); ++i) {
data_to(i) = data_to(i) > data_from(i) ? data_to(i) : data_from(i);
}
}
int main()
{
GridMap map;
map.setGeometry(Length(20.0, 20.0), 0.004, Position(0.0, 0.0));
map.add("random");
map["random"].setRandom();
map.add("layer1", 0.0);
map.add("layer2", 0.0);
map.add("layer3", 0.0);
map.add("layer4", 0.0);
map.add("layer5", 0.0);
map.add("layer6", 0.0);
cout << "Results for iteration over " << map.getSize()(0) << " x " << map.getSize()(1) << " (" << map.getSize().prod() << ") grid cells." << endl;
cout << "=========================================" << endl;
clk::time_point t1 = clk::now();
runGridMapIteratorVersion1(map, "random", "layer1");
clk::time_point t2 = clk::now();
cout << "Duration grid map iterator (convenient use): " << duration(t2 - t1) << " ms" << endl;
t1 = clk::now();
runGridMapIteratorVersion2(map, "random", "layer2");
t2 = clk::now();
cout << "Duration grid map iterator (direct access to data layers): " << duration(t2 - t1) << " ms" << endl;
t1 = clk::now();
runGridMapIteratorVersion3(map, "random", "layer3");
t2 = clk::now();
cout << "Duration grid map iterator (linear index): " << duration(t2 - t1) << " ms" << endl;
t1 = clk::now();
runEigenFunction(map, "random", "layer4");
t2 = clk::now();
cout << "Duration Eigen function: " << duration(t2 - t1) << " ms" << endl;
t1 = clk::now();
runCustomIndexIteration(map, "random", "layer5");
t2 = clk::now();
cout << "Duration custom index iteration: " << duration(t2 - t1) << " ms" << endl;
t1 = clk::now();
runCustomLinearIndexIteration(map, "random", "layer6");
t2 = clk::now();
cout << "Duration custom linear index iteration: " << duration(t2 - t1) << " ms" << endl;
return 0;
}