Always do BOOST_FOREACH with a reference if you can! In this case, made a noticeable difference in performance by avoiding hundreds of thousands of mallocs.

release/4.3a0
Frank Dellaert 2010-03-11 21:40:56 +00:00
parent 84c25b2346
commit 1cba03a490
3 changed files with 6 additions and 9 deletions

View File

@ -29,10 +29,6 @@
/*template boost::shared_ptr<F> removeAndCombineFactors(FactorGraph<F>&, const std::string&);*/ \
template FactorGraph<F> combine(const FactorGraph<F>&, const FactorGraph<F>&);
// trick from some reading group
#define FOREACH_PAIR( KEY, VAL, COL) BOOST_FOREACH (boost::tie(KEY,VAL),COL)
using namespace std;
namespace gtsam {
@ -340,7 +336,7 @@ PredecessorMap<Key> FactorGraph<Factor>::findMinimumSpanningTree() const {
template<class Factor> template <class Key, class Factor2>
void FactorGraph<Factor>::split(const PredecessorMap<Key>& tree, FactorGraph<Factor>& Ab1, FactorGraph<Factor>& Ab2) const{
BOOST_FOREACH(sharedFactor factor, factors_){
BOOST_FOREACH(const sharedFactor& factor, factors_){
if (factor->keys().size() > 2)
throw(invalid_argument("split: only support factors with at most two keys"));

View File

@ -25,7 +25,7 @@ namespace gtsam {
template<class Config>
Vector NonlinearFactorGraph<Config>::unwhitenedError(const Config& c) const {
list<Vector> errors;
BOOST_FOREACH(typename NonlinearFactorGraph<Config>::sharedFactor factor, this->factors_)
BOOST_FOREACH(const sharedFactor& factor, this->factors_)
errors.push_back(factor->unwhitenedError(c));
return concatVectors(errors);
}
@ -35,7 +35,7 @@ namespace gtsam {
double NonlinearFactorGraph<Config>::error(const Config& c) const {
double total_error = 0.;
// iterate over all the factors_ to accumulate the log probabilities
BOOST_FOREACH(typename NonlinearFactorGraph<Config>::sharedFactor factor, this->factors_)
BOOST_FOREACH(const sharedFactor& factor, this->factors_)
total_error += factor->error(c);
return total_error;
}
@ -49,8 +49,7 @@ namespace gtsam {
boost::shared_ptr<GaussianFactorGraph> linearFG(new GaussianFactorGraph);
// linearize all factors
typedef typename NonlinearFactorGraph<Config>::sharedFactor Factor;
BOOST_FOREACH(const Factor& factor, this->factors_) {
BOOST_FOREACH(const sharedFactor& factor, this->factors_) {
boost::shared_ptr<GaussianFactor> lf = factor->linearize(config);
linearFG->push_back(lf);
}

View File

@ -30,6 +30,8 @@ namespace gtsam {
public:
typedef typename boost::shared_ptr<NonlinearFactor<Config> > sharedFactor;
/** unnormalized error */
double error(const Config& c) const;