I am Charmie

メモとログ

Eigen: select columns with N largest norm 2

PTAM/Build/Linux/VideoSource_Linux_OpenCV.ccThis post is to measure the computation time of the previous post, in which I posted a C++ code using Eigen to select columns with N largest norm. My code is available here.

Suppose we define a function doing the column selection. It's natural using the function is slower than column selection without using the function, meaning write the selection code in main function. My question is how slower using function is. So, the following code compares two methods: (1) column selection implemented in main function and (2) column selection executed as a function. I tried with the following two cases.

  1. Case 1.
    • an original matrix of size 100x100000
    • the number of selected columns: 10
    • the number of trials: 100
  2. Case 2
    • an original matrix of size 100000x100
    • the number of selected columns: 10
    • the number of trials: 100
Case 1: result Elapsed time w/o using function = 75.2322 sec. for 100 trial Elapsed time w/ using function = 75.2383 sec. for 100 trial
Case 2: result Elapsed time w/o using function = 75.3003 sec. for 100 trial Elapsed time w/ using function = 89.1946 sec. for 100 trial

[code lang="cpp"]

include <Eigen/Core>;

include <iostream>;

include <vector>;

include <ctime>;

Eigen::MatrixXd func( const Eigen::MatrixXd &m, const int numDataSub ) { int dimData = m.rows(); Eigen::MatrixXd mSub = Eigen::MatrixXd::Zero(dimData, numDataSub); Eigen::VectorXd mNorm = m.colwise().norm(); std::vector<int>; indexMax(numDataSub); for(int n = 0; n < numDataSub; ++n) { mNorm.maxCoeff(&indexMax[n]); mNorm(indexMax[n]) = 0.0; mSub.col(n) = m.col(indexMax[n]); } return mSub; }

int main() { std::clock_t timeStart; std::clock_t timeEnd; int numTrial = 100;

int dimData = 100;
int numData = 100000;
int numDataSub = 50;
Eigen::MatrixXd m;
Eigen::MatrixXd mSub = Eigen::MatrixXd::Zero(dimData, numDataSub);
Eigen::VectorXd mNorm;
std::vector&lt;int&gt;; indexMax(numDataSub);

// std::cout &lt;&lt; &quot;All data and its norm:&quot; &lt;&lt; std::endl;
// for(int n = 0; n &lt; numData; ++n)
// {
//  std::cout &lt;&lt; &quot;   data[&quot; &lt;&lt; n &lt;&lt; &quot;] = (&quot; &lt;&lt; m.col(n).transpose() &lt;&lt; &quot;)    norm = &quot; &lt;&lt; mNorm(n) &lt;&lt; std::endl;     
// }

// retrieve columns whose norm  is top numDataSub

///
// Method 1: column selection without using a function
timeStart = std::clock();
for(int t = 0; t &lt; numTrial; ++t)
{
    m = Eigen::MatrixXd::Random(dimData, numData);
    mNorm = m.colwise().norm();
    for(int n = 0; n &lt; numDataSub; ++n)
    {
        mNorm.maxCoeff(&amp;indexMax[n]);
        mNorm(indexMax[n]) = 0.0;
        mSub.col(n) = m.col(indexMax[n]);
    }
    // std::cout &lt;&lt; &quot;Selected data with &quot; &lt;&lt; numDataSub &lt;&lt; &quot;largest norm:&quot; &lt;&lt; std::endl;
    // for(int n = 0; n &lt; numDataSub; ++n)
    // {
    //  std::cout &lt;&lt; &quot;   data[&quot; &lt;&lt; n &lt;&lt; &quot;] = (&quot; &lt;&lt; mSub.col(n).transpose() &lt;&lt; &quot;) original index = &quot; &lt;&lt; indexMax[n] &lt;&lt; std::endl;        
    // }
}
timeEnd = std::clock();
std::cout &lt;&lt; &quot;Elapsed time w/o using function = &quot; &lt;&lt; (double)(timeEnd - timeStart)/CLOCKS_PER_SEC &lt;&lt; &quot; sec. for &quot; &lt;&lt; numTrial &lt;&lt; &quot; trial&quot; &lt;&lt; std::endl;

///
// Method 2: column selection using a function
timeStart = std::clock();
for(int t = 0; t &lt; numTrial; ++t)
{
    m = Eigen::MatrixXd::Random(dimData, numData);
    mSub = func(m, numDataSub);
}
timeEnd = std::clock();
std::cout &lt;&lt; &quot;Elapsed time w/  using function = &quot; &lt;&lt; (double)(timeEnd - timeStart)/CLOCKS_PER_SEC &lt;&lt; &quot; sec. for &quot; &lt;&lt; numTrial &lt;&lt; &quot; trial&quot; &lt;&lt; std::endl;

return 0;

} [/code]