[mlpack-git] master: Minor/trivial speedup: preallocate the matrix. (6e98f6d)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Wed Jun 17 17:08:53 EDT 2015


Repository : https://github.com/mlpack/mlpack

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/98c0c483a3547c8f49cdfe38670a603bd29036a0...6e98f6d5e61ac0ca861f0a7c3ec966076eccc50e

>---------------------------------------------------------------

commit 6e98f6d5e61ac0ca861f0a7c3ec966076eccc50e
Author: Ryan Curtin <ryan at ratml.org>
Date:   Wed Jun 17 17:08:39 2015 -0400

    Minor/trivial speedup: preallocate the matrix.


>---------------------------------------------------------------

6e98f6d5e61ac0ca861f0a7c3ec966076eccc50e
 src/mlpack/methods/mean_shift/mean_shift_impl.hpp | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/src/mlpack/methods/mean_shift/mean_shift_impl.hpp b/src/mlpack/methods/mean_shift/mean_shift_impl.hpp
index 634dcaf..46499ba 100644
--- a/src/mlpack/methods/mean_shift/mean_shift_impl.hpp
+++ b/src/mlpack/methods/mean_shift/mean_shift_impl.hpp
@@ -103,15 +103,26 @@ void MeanShift<UseKernel, KernelType, MatType>::GenSeeds(
       allSeeds[binnedPoint]++;
   }
 
-  // Remove seeds with too few points.
+  // Remove seeds with too few points.  First we count the number of seeds we
+  // end up with, then we add them.
   std::map<VecType, int, less<VecType> >::iterator it;
+  size_t count = 0;
+  for (it = allSeeds.begin(); it != allSeeds.end(); ++it)
+    if (it->second >= minFreq)
+      ++count;
+
+  seeds.set_size(data.n_rows, count);
+  count = 0;
   for (it = allSeeds.begin(); it != allSeeds.end(); ++it)
   {
     if (it->second >= minFreq)
-      seeds.insert_cols(seeds.n_cols, it->first);
+    {
+      seeds.col(count) = it->first;
+      ++count;
+    }
   }
 
-  seeds = seeds * binSize;
+  seeds *= binSize;
 }
 
 // Calculate new centroid with given kernel.



More information about the mlpack-git mailing list