iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tests/test-PointCloud2Speed.C
/*! @file
@author Shane Grant
@copyright GNU Public License (GPL v3)
@section License
@verbatim
// ////////////////////////////////////////////////////////////////////////
// The iLab Neuromorphic Robotics Toolkit (NRT) //
// Copyright 2010-2012 by the University of Southern California (USC) //
// and the iLab at USC. //
// //
// iLab - University of Southern California //
// Hedco Neurociences Building, Room HNB-10 //
// Los Angeles, Ca 90089-2520 - USA //
// //
// See http://ilab.usc.edu for information about this project. //
// ////////////////////////////////////////////////////////////////////////
// This file is part of The iLab Neuromorphic Robotics Toolkit. //
// //
// The iLab Neuromorphic Robotics Toolkit is free software: you can //
// redistribute it and/or modify it under the terms of the GNU General //
// Public License as published by the Free Software Foundation, either //
// version 3 of the License, or (at your option) any later version. //
// //
// The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
// that it will be useful, but WITHOUT ANY WARRANTY; without even the //
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
// PURPOSE. See the GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with The iLab Neuromorphic Robotics Toolkit. If not, see //
// <http://www.gnu.org/licenses/>. //
// ////////////////////////////////////////////////////////////////////////
@endverbatim */
#include <nrt/config.h>
#ifdef NRT_HAVE_CLOUD
#include <algorithm>
using namespace nrt;
template <class T>
struct Wrapper
{
Wrapper( T* && t ) : itsT(t) {}
T operator*()
{
return *itsT;
}
T operator[]( size_t index )
{
return itsT[index];
}
T * itsT;
};
template <class Func>
double doSomething( size_t amount, Func && f )
{
Timer t;
t.reset();
for( size_t i = 0; i < amount; ++i )
f();
return t.getreset().count() / amount * 1000.0;
}
int main( int argc, char ** argv )
{
float x = 3.4f;
Wrapper<float> w( &x );
std::cout << *w << std::endl;
std::cout << w[0] << std::endl;
try
{
// If numAverages is increased, the average time will decrease as most of the functionality here benefits from
// caching and the first execution will be more expensive
const size_t numAverages = 10;
const size_t resizeSize = 1000000;
NRT_INFO( "Averaging times over " << numAverages << " runs with size " << resizeSize << " all times in milliseconds" );
PointCloud2 cloud = PointCloud2::create<int, double, char, SparseField<bool>>();
NRT_INFO( "Resize time " << doSomething( numAverages, [&](){ cloud.resize( 0 ); cloud.resize( resizeSize ); } ) );
cloud.resize( resizeSize );
NRT_INFO( "Iterate time (geo read) " << doSomething( numAverages, [&]()
{
for( auto i = cloud.geometry_const_begin(), end = cloud.geometry_const_end(); i != end; ++i ) (void)*i;
} ) );
NRT_INFO( "Iterate time (geo read) slow way " << doSomething( numAverages, [&]()
{
for( auto i = cloud.geometry_const_begin(); i != cloud.geometry_const_end(); ++i ) (void)*i;
} ) );
NRT_INFO( "Iterate time (geo write) " << doSomething( numAverages, [&]()
{
for( auto i = cloud.geometry_begin(), end = cloud.geometry_end(); i != end; ++i )
i->x() = 3;
} ) );
NRT_INFO( "Iterate time (dense read (0fields)) " << doSomething( numAverages, [&]()
{
for( auto i = cloud.const_begin(), end=cloud.const_end(); i != end; ++i ) (void)*i;
} ) );
NRT_INFO( "Iterate time (dense read (1fields)) " << doSomething( numAverages, [&]()
{
for( auto i = cloud.const_begin<int>(), end = cloud.const_end<int>(); i != end; ++i ) (void)*i;
} ) );
NRT_INFO( "Iterate time (dense read (2fields)) " << doSomething( numAverages, [&]()
{
for( auto i = cloud.const_begin<int, double>(), end = cloud.const_end<int, double>(); i != end; ++i ) (void)*i;
} ) );
NRT_INFO( "Iterate time (dense read (3fields)) " << doSomething( numAverages, [&]()
{
for( auto i = cloud.const_begin<int, double, char>(), end = cloud.const_end<int, double, char>(); i != end; ++i ) (void)*i;
} ) );
NRT_INFO( "Iterate time (dense read (2fields) slow way) " << doSomething( numAverages, [&]()
{
for( auto i = cloud.const_begin<int, double>(); i != cloud.const_end<int, double>(); ++i ) (void)*i;
} ) );
NRT_INFO( "Iterate time (dense write (0fields)) " << doSomething( numAverages, [&]()
{
for( auto i = cloud.begin(), end=cloud.end(); i != end; ++i )
i->geometry().x() = 1;
} ) );
NRT_INFO( "Iterate time (dense write (1fields)) " << doSomething( numAverages, [&]()
{
for( auto i = cloud.begin<int>(), end = cloud.end<int>(); i != end; ++i )
i->get<int>() = 1;
} ) );
NRT_INFO( "Iterate time (dense write (2fields)) " << doSomething( numAverages, [&]()
{
for( auto i = cloud.begin<int, double>(), end = cloud.end<int, double>(); i != end; ++i )
i->get<double>() = 1.0;
} ) );
NRT_INFO( "Iterate time (dense write (2fields) slow way) " << doSomething( numAverages, [&]()
{
for( auto i = cloud.begin<int, double>(); i != cloud.end<int, double>(); ++i )
i->get<double>() = 1.0;
} ) );
Indices indices(cloud.size());
std::iota( indices.begin(), indices.end(), 0 );
std::random_shuffle( indices.begin(), indices.end() );
NRT_INFO( "Iterate time (subset read) " << doSomething( numAverages, [&]()
{
for( auto i = cloud.subset_const_begin( indices ), end = cloud.subset_const_end( indices ); i != end; ++i )
(void) *i;
} ) );
std::random_shuffle( indices.begin(), indices.end() );
auto ii3 = cloud.subset_const_begin( indices );
auto jj3 = cloud.subset_const_end( indices );
NRT_INFO( "Iterate time (subset read, creation not measured ) " << doSomething( numAverages, [&]()
{
for( ; ii3 != jj3; ++ii3 ) (void)*ii3;
} ) );
std::random_shuffle( indices.begin(), indices.end() );
NRT_INFO( "Iterate time (subset write) " << doSomething( numAverages, [&]()
{
for( auto i = cloud.subset_begin( indices ), end = cloud.subset_end( indices ); i != end; ++i )
i->geometry().x() = 1;
} ) );
NRT_INFO( "Ref count check test unique access off " << doSomething( numAverages, [&]()
{
for( size_t i = 0; i < 1000000; ++i )
{
PointCloud2::Geometry & bla = cloud[i%2];
bla.x();
}
} ) );
NRT_INFO( "Ref count check test unique access off const " << doSomething( numAverages, [&]()
{
for( size_t i = 0; i < 1000000; ++i )
{
PointCloud2::Geometry const & bla = const_cast<PointCloud2 const &>(cloud)[i%2];
bla.x();
}
} ) );
}
catch( ... )
{
}
return 0;
}
#else
int main( int argc, char ** argv )
{
return 1;
}
#endif // NRT_HAVE_CLOUD