This forum has been archived. All content is frozen. Please use KDE Discuss instead.

why is eigen select slow?

Tags: None
(comma "," separated)
johnm1019
Registered Member
Posts
46
Karma
0

why is eigen select slow?

Thu Jul 28, 2011 2:59 pm
I was benchmarking the difference between a basic if statement and replace and eigen select and eigen select takes almost 1.5 times as long. How can this be?

Here is my code
Code: Select all
   LARGE_INTEGER firstTime, secondTime, liFreq;
   QueryPerformanceFrequency(&liFreq);
   QueryPerformanceCounter(&firstTime);
   srand(firstTime.LowPart);
   int vecSize = 3000*50*50*50;
   std::cout << "starting with " << vecSize << " voxels" << std::endl;

   // create the base dataset
   Eigen::Array<float, Eigen::Dynamic, 1> m_filteredResult, columnSums, originalAnswer;
   m_filteredResult.setRandom(vecSize);
   originalAnswer.setZero(vecSize);
   columnSums.setRandom(vecSize);
   float theQualityFilterThreshold = static_cast<float>(rand())/static_cast<float>(RAND_MAX);
   int numFiltered = 0;
   std::cout << "threshold: " << theQualityFilterThreshold << " colSum-max: " << columnSums.maxCoeff() << std::endl;

   // time the original method
   QueryPerformanceCounter(&firstTime);
   for( int i=0; i<m_filteredResult.size(); i++ )
   {
      if( columnSums.coeff(i) < theQualityFilterThreshold )
      {
         m_filteredResult(i) = 0.0f;
      }
   }
   QueryPerformanceCounter(&secondTime);
   std::cout << "original method took: " << 1000.0f*(secondTime.QuadPart-firstTime.QuadPart)/liFreq.QuadPart << "ms" << std::endl;
   originalAnswer = m_filteredResult;

   // time the new method
   QueryPerformanceCounter(&firstTime);
   m_filteredResult = (columnSums < theQualityFilterThreshold ).select(0.0f, m_filteredResult);
   QueryPerformanceCounter(&secondTime);
   std::cout << "select method took: " << 1000.0f*(secondTime.QuadPart-firstTime.QuadPart)/liFreq.QuadPart << "ms" << std::endl;

   // check the difference
   printf("numerical difference between methods: %.8f \n", (originalAnswer-m_filteredResult).cwiseAbs().sum() );


and the output (these values are representative, I've run it and averaged over 30 trials to be sure)
Code: Select all
starting with 375000000 voxels
threshold: 0.509812 colSum-max: 1
original method took: 1746.73ms
select method took: 2581.42ms
numerical difference between methods: 0.00000000


Running on an i7 920, visual studio 2010, 64 bit, all optimization enabled in release mode. Code is Eigen trunk rev. 4166

Eigen is always soooo much faster than the code I write myself -- I feel like I must be forgetting something given the simple case.

As the threshold changes and the total operation time rises due to the two different branching proportions, the ratio between mine and eigen stays almost perfectly at ~1.5
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: why is eigen select slow?

Fri Aug 12, 2011 9:17 pm
this is probably because the equivalent code is:

for( int i=0; i<m_filteredResult.size(); i++ )
{
m_filteredResult(i) = (columnSums.coeff(i) < theQualityFilterThreshold)
? 0.0f
: m_filteredResult(i);
}

and MSVC is perhaps not able to simplify this because of all the abstraction layer. Checking the generated assembly would help to see what's going on.


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Google [Bot], Yahoo [Bot]