Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8203793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:41:06+00:00 2026-06-07T07:41:06+00:00

I’ve written a routine that uses std::vector<double> rather heavily. It runs rather slowly and

  • 0

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)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:41:07+00:00Added an answer on June 7, 2026 at 7:41 am

    This is because you are passing your function arguments by value. Every time a std::vector is passed by value, a complete copy of the vector must be made.

    Change these:

    double SpecRatio::VValue(vector<double>V, int Index) {
    
    double SpecRatio::RatioPoint(vector<double>Num, vector<double>Denom...
    

    To:

    double SpecRatio::VValue(const vector<double> &V, int Index)
    
    double SpecRatio::RatioPoint(const vector<double> &Num, const vector<double> &Denom...
    

    Because for your usage, you never actually needed separate copies of these vectors to be made.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.