33 #include <gtest/gtest.h> 39 TEST(BlockmemGridmap, SpacialAccessPerformance)
41 constexpr
int size[3] =
45 constexpr
int pad[3] =
49 constexpr
int range = 0x10;
50 constexpr
int repeat = 4;
56 using ThreeDimArrayFloat = std::array<std::array<std::array<float, size[0]>, size[1]>, size[2]>;
59 std::shared_ptr<ThreeDimArrayFloat> array_ptr(
new ThreeDimArrayFloat);
60 std::shared_ptr<ThreeDimArrayFloat> array_ret_ptr(
new ThreeDimArrayFloat);
61 ThreeDimArrayFloat& array = *array_ptr;
62 ThreeDimArrayFloat& array_ret = *array_ret_ptr;
64 gm.
reset(
Vec(size[0], size[1], size[2]));
65 gm_ret.
reset(
Vec(size[0], size[1], size[2]));
69 for (i[0] = 0; i[0] < size[0]; ++i[0])
71 for (i[1] = 0; i[1] < size[1]; ++i[1])
73 for (i[2] = 0; i[2] < size[2]; ++i[2])
75 gm[i] = i[2] * 0x100 + i[1] * 0x10 + i[0];
76 array[i[2]][i[1]][i[0]] = gm[i];
81 for (i[0] = pad[0]; i[0] < size[0] - pad[0]; ++i[0])
83 for (i[1] = pad[1]; i[1] < size[1] - pad[1]; ++i[1])
85 for (i[2] = pad[2]; i[2] < size[2] - pad[2]; ++i[2])
87 ASSERT_EQ(gm[i], array[i[2]][i[1]][i[0]]);
91 boost::chrono::duration<float> d0;
92 boost::chrono::duration<float> d1;
94 for (
int r = 0; r < repeat; ++r)
97 const auto ts0 = boost::chrono::high_resolution_clock::now();
98 for (i[0] = pad[0]; i[0] < size[0] - pad[0]; ++i[0])
100 for (i[1] = pad[1]; i[1] < size[1] - pad[1]; ++i[1])
102 for (i[2] = pad[2]; i[2] < size[2] - pad[2]; ++i[2])
107 for (j[0] = -range; j[0] <= range; ++j[0])
109 for (j[1] = -range; j[1] <= range; ++j[1])
111 for (j[2] = -range; j[2] <= range; ++j[2])
113 const Vec ij = i + j;
123 std::cerr << std::endl;
124 const auto te0 = boost::chrono::high_resolution_clock::now();
125 d0 += boost::chrono::duration<float>(te0 - ts0);
128 const auto ts1 = boost::chrono::high_resolution_clock::now();
129 for (i[0] = pad[0]; i[0] < size[0] - pad[0]; ++i[0])
131 for (i[1] = pad[1]; i[1] < size[1] - pad[1]; ++i[1])
133 for (i[2] = pad[2]; i[2] < size[2] - pad[2]; ++i[2])
136 array_ret[i[2]][i[1]][i[0]] = 0;
138 for (j[0] = -range; j[0] <= range; ++j[0])
140 for (j[1] = -range; j[1] <= range; ++j[1])
142 for (j[2] = -range; j[2] <= range; ++j[2])
144 const Vec ij = i + j;
145 array_ret[i[2]][i[1]][i[0]] += array[ij[2]][ij[1]][ij[0]];
146 array[ij[2]][ij[1]][ij[0]]++;
154 std::cerr << std::endl;
155 const auto te1 = boost::chrono::high_resolution_clock::now();
156 d1 += boost::chrono::duration<float>(te1 - ts1);
158 std::cout <<
"BlockMemGridmap<3, 2>: " << d0.count() << std::endl;
159 std::cout <<
"Array[][][]: " << d1.count() << std::endl;
162 for (i[0] = pad[0]; i[0] < size[0] - pad[0]; ++i[0])
164 for (i[1] = pad[1]; i[1] < size[1] - pad[1]; ++i[1])
166 for (i[2] = pad[2]; i[2] < size[2] - pad[2]; ++i[2])
168 ASSERT_EQ(gm_ret[i], array_ret[i[2]][i[1]][i[0]]);
174 std::cout <<
"Improvement ratio: " << d1.count() / d0.count() << std::endl;
179 int main(
int argc,
char** argv)
181 testing::InitGoogleTest(&argc, argv);
183 return RUN_ALL_TESTS();
void reset(const CyclicVecInt< DIM, NONCYCLIC > &size)
TEST(BlockmemGridmap, BlockWidth)
MotionPrimitiveBuilder::Vec Vec