iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-PointCloud2Speed.C
Go to the documentation of this file.
1 /*! @file
2  @author Shane Grant
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 #include <nrt/config.h>
36 #ifdef NRT_HAVE_CLOUD
37 
39 #include <nrt/Core/Typing/Time.H>
40 #include <nrt/Core/Debugging/Log.H>
41 #include <algorithm>
42 
43 using namespace nrt;
44 
45 template <class T>
46 struct Wrapper
47 {
48  Wrapper( T* && t ) : itsT(t) {}
49 
50  T operator*()
51  {
52  return *itsT;
53  }
54 
55  T operator[]( size_t index )
56  {
57  return itsT[index];
58  }
59 
60  T * itsT;
61 };
62 
63 template <class Func>
64 double doSomething( size_t amount, Func && f )
65 {
66  Timer t;
67  t.reset();
68 
69  for( size_t i = 0; i < amount; ++i )
70  f();
71 
72  return t.getreset().count() / amount * 1000.0;
73 }
74 
75 int main( int argc, char ** argv )
76 {
77  float x = 3.4f;
78  Wrapper<float> w( &x );
79 
80  std::cout << *w << std::endl;
81  std::cout << w[0] << std::endl;
82 
83 
84 
85  try
86  {
87  // If numAverages is increased, the average time will decrease as most of the functionality here benefits from
88  // caching and the first execution will be more expensive
89  const size_t numAverages = 10;
90  const size_t resizeSize = 1000000;
91 
92  NRT_INFO( "Averaging times over " << numAverages << " runs with size " << resizeSize << " all times in milliseconds" );
93 
94  PointCloud2 cloud = PointCloud2::create<int, double, char, SparseField<bool>>();
95 
96  NRT_INFO( "Resize time " << doSomething( numAverages, [&](){ cloud.resize( 0 ); cloud.resize( resizeSize ); } ) );
97  cloud.resize( resizeSize );
98 
99  NRT_INFO( "Iterate time (geo read) " << doSomething( numAverages, [&]()
100  {
101  for( auto i = cloud.geometry_const_begin(), end = cloud.geometry_const_end(); i != end; ++i ) (void)*i;
102  } ) );
103 
104  NRT_INFO( "Iterate time (geo read) slow way " << doSomething( numAverages, [&]()
105  {
106  for( auto i = cloud.geometry_const_begin(); i != cloud.geometry_const_end(); ++i ) (void)*i;
107  } ) );
108 
109  NRT_INFO( "Iterate time (geo write) " << doSomething( numAverages, [&]()
110  {
111  for( auto i = cloud.geometry_begin(), end = cloud.geometry_end(); i != end; ++i )
112  i->x() = 3;
113  } ) );
114 
115  NRT_INFO( "Iterate time (dense read (0fields)) " << doSomething( numAverages, [&]()
116  {
117  for( auto i = cloud.const_begin(), end=cloud.const_end(); i != end; ++i ) (void)*i;
118  } ) );
119 
120  NRT_INFO( "Iterate time (dense read (1fields)) " << doSomething( numAverages, [&]()
121  {
122  for( auto i = cloud.const_begin<int>(), end = cloud.const_end<int>(); i != end; ++i ) (void)*i;
123  } ) );
124 
125  NRT_INFO( "Iterate time (dense read (2fields)) " << doSomething( numAverages, [&]()
126  {
127  for( auto i = cloud.const_begin<int, double>(), end = cloud.const_end<int, double>(); i != end; ++i ) (void)*i;
128  } ) );
129 
130  NRT_INFO( "Iterate time (dense read (3fields)) " << doSomething( numAverages, [&]()
131  {
132  for( auto i = cloud.const_begin<int, double, char>(), end = cloud.const_end<int, double, char>(); i != end; ++i ) (void)*i;
133  } ) );
134 
135  NRT_INFO( "Iterate time (dense read (2fields) slow way) " << doSomething( numAverages, [&]()
136  {
137  for( auto i = cloud.const_begin<int, double>(); i != cloud.const_end<int, double>(); ++i ) (void)*i;
138  } ) );
139 
140 
141  NRT_INFO( "Iterate time (dense write (0fields)) " << doSomething( numAverages, [&]()
142  {
143  for( auto i = cloud.begin(), end=cloud.end(); i != end; ++i )
144  i->geometry().x() = 1;
145  } ) );
146 
147  NRT_INFO( "Iterate time (dense write (1fields)) " << doSomething( numAverages, [&]()
148  {
149  for( auto i = cloud.begin<int>(), end = cloud.end<int>(); i != end; ++i )
150  i->get<int>() = 1;
151  } ) );
152 
153  NRT_INFO( "Iterate time (dense write (2fields)) " << doSomething( numAverages, [&]()
154  {
155  for( auto i = cloud.begin<int, double>(), end = cloud.end<int, double>(); i != end; ++i )
156  i->get<double>() = 1.0;
157  } ) );
158 
159  NRT_INFO( "Iterate time (dense write (2fields) slow way) " << doSomething( numAverages, [&]()
160  {
161  for( auto i = cloud.begin<int, double>(); i != cloud.end<int, double>(); ++i )
162  i->get<double>() = 1.0;
163  } ) );
164 
165  Indices indices(cloud.size());
166  std::iota( indices.begin(), indices.end(), 0 );
167  std::random_shuffle( indices.begin(), indices.end() );
168 
169  NRT_INFO( "Iterate time (subset read) " << doSomething( numAverages, [&]()
170  {
171  for( auto i = cloud.subset_const_begin( indices ), end = cloud.subset_const_end( indices ); i != end; ++i )
172  (void) *i;
173  } ) );
174 
175  std::random_shuffle( indices.begin(), indices.end() );
176 
177  auto ii3 = cloud.subset_const_begin( indices );
178  auto jj3 = cloud.subset_const_end( indices );
179 
180  NRT_INFO( "Iterate time (subset read, creation not measured ) " << doSomething( numAverages, [&]()
181  {
182  for( ; ii3 != jj3; ++ii3 ) (void)*ii3;
183  } ) );
184 
185  std::random_shuffle( indices.begin(), indices.end() );
186 
187  NRT_INFO( "Iterate time (subset write) " << doSomething( numAverages, [&]()
188  {
189  for( auto i = cloud.subset_begin( indices ), end = cloud.subset_end( indices ); i != end; ++i )
190  i->geometry().x() = 1;
191  } ) );
192 
193  NRT_INFO( "Ref count check test unique access off " << doSomething( numAverages, [&]()
194  {
195  for( size_t i = 0; i < 1000000; ++i )
196  {
197  PointCloud2::Geometry & bla = cloud[i%2];
198  bla.x();
199  }
200  } ) );
201 
202  NRT_INFO( "Ref count check test unique access off const " << doSomething( numAverages, [&]()
203  {
204  for( size_t i = 0; i < 1000000; ++i )
205  {
206  PointCloud2::Geometry const & bla = const_cast<PointCloud2 const &>(cloud)[i%2];
207  bla.x();
208  }
209  } ) );
210  }
211  catch( ... )
212  {
213  }
214 
215  return 0;
216 }
217 #else
218 int main( int argc, char ** argv )
219 {
220  return 1;
221 }
222 #endif // NRT_HAVE_CLOUD