std::vector optimisation required

Posted by marcp on Stack Overflow See other posts from Stack Overflow or by marcp
Published on 2012-07-10T03:01:53Z Indexed on 2012/07/10 3:15 UTC
Read the original article Hit count: 226

Filed under:
|
|

I've written a routine that uses std::vector<double> rather heavily. It runs rather slowly and AQTime seems to imply that I am constructing mountains of vectors but I'm not sure why I would be. For some context, my sample run iterates 10 times. Each iteration copies 3 c arrays of ~400 points into vectors and creates 3 new same sized vectors for output. Each output point might be the result of summing up to 20 points from 2 of the input vectors, which works out to a worst case of 10*400*3*2*20 = 480,000 dereferences. Incredibly the profiler indicates that some of the std:: methods are being called 46 MILLION times. I suspect I'm doing something wrong!

Some code:

vector<double>gdbChannel::GetVector() {
   if (fHaveDoubleData & (fLength > 0)) {
      double * pD = getDoublePointer();
      vector<double>v(pD, pD + fLength);

      return v;
   } else {
      throw(Exception("attempt to retrieve vector on empty line")); ;
   }
}

void gdbChannel::SaveVector(GX_HANDLE _hLine, const vector<double> & V) {
   if (hLine != _hLine) {
      GetLine(_hLine, V.size(), true);
   }
   GX_DOUBLE * pData = getDoublePointer();
   memcpy(pData, &V[0], V.size()*sizeof(V[0]));
   ReplaceData();
}

///This routine gets called 10 times
 bool SpecRatio::DoWork(GX_HANDLE_PTR pLine) {
   if (!(hKin.GetLine(*pLine, true) && hUin.GetLine(*pLine, true) && hTHin.GetLine(*pLine, true))) {
      return true;
   }
   vector<double>vK = hKin.GetVector();
   vector<double>vU = hUin.GetVector();
   vector<double>vTh = hTHin.GetVector();

   if ((vK.size() == 0) || (vU.size() == 0) || (vTh.size() == 0)) {
      return true;
   }
   ///TODO: confirm all vectors the same lenghth
   len = vK.size();
   vUK.clear();  // these 3 vectors are declared as private class members
   vUTh.clear();
   vThK.clear();
   vUK.reserve(len);
   vUTh.reserve(len);
   vThK.reserve(len);

   // TODO: ensure everything is same fidincr, fidstart and length

   for (int i = 0; i < len; i++) {
      if (vK.at(i) < MinK) {
         vUK.push_back(rDUMMY);
         vUTh.push_back(rDUMMY);
         vThK.push_back(rDUMMY);
      } else {
         vUK.push_back(RatioPoint(vU, vK, i, UMin, KMin));
         vUTh.push_back(RatioPoint(vU, vTh, i, UMin, ThMin));
         vThK.push_back(RatioPoint(vTh, vK, i, ThMin, KMin));
      }

   }
   hUKout.setFidParams(hKin);
   hUKout.SaveVector(*pLine, vUK);
   hUTHout.setFidParams(hKin);
   hUTHout.SaveVector(*pLine, vUTh);
   hTHKout.setFidParams(hKin);
   hTHKout.SaveVector(*pLine, vThK);
   return TestError();
}

double SpecRatio::VValue(vector<double>V, int Index) {
   double result;
   if ((Index < 0) || (Index >= len)) {
      result = 0;

   } else {
      try {
         result = V.at(Index);
         if (OasisUtils::isDummy(result)) {
            result = 0;
         }
      }
      catch (out_of_range) {
         result = 0;
      }
   }
   return result;
}

double SpecRatio::RatioPoint(vector<double>Num, vector<double>Denom, int Index, double NumMin, double DenomMin) {
   double num = VValue(Num, Index);
   double denom = VValue(Denom, Index);
   int s = 0;
   // Search equalled 10 in this case
   while (((num < NumMin) || (denom < DenomMin)) && (s < Search)) {
      num += VValue(Num, Index - s) + VValue(Num, Index + s);
      denom += VValue(Denom, Index - s) + VValue(Denom, Index + s);
      s++;
   }
   if ((num < NumMin) || (denom < DenomMin)) {
      return rDUMMY;
   } else {
      return num / denom;
   }

}

The top AQTime offenders are:

std::_Uninit_copy >, double *, std::allocator > 3.65 secs and 115731 Hits

std::_Construct 1.69 secs and 46450637 Hits

std::_Vector_const_iterator >::operator !=1.66 secs and 46566395 Hits and so on...

std::allocator<double>::construct,
operator new,
std::_Vector_const_iterator<double, std::allocator<double> >::operator ++, std::_Vector_const_iterator<double, std::allocator<double> >::operator * std::_Vector_const_iterator<double, std::allocator<double> >::operator ==

each get called over 46 million times.

I'm obviously doing something wrong to cause all these objects to be created. Can anyone see my error(s)?

© Stack Overflow or respective owner

Related posts about c++

Related posts about std