我编写了一个例程,该例程大量使用std::vector<double>
。它运行相当缓慢,并且AQTime似乎暗示我正在构建大量的向量,但不确定为什么会这样。在某些情况下,我的示例运行迭代10次。每次迭代将3个c点数组(约400个点)复制到向量中,并创建3个相同大小的向量以进行输出。每个输出点可能是两个输入向量中最多20个点相加的结果,最坏的情况是10 * 400 * 3 * 2 * 20 = 480,000个引用。令人难以置信的是,探查器指示某些std ::方法被调用了4,600万次。我怀疑我做错了!
一些代码:
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;
}
}
最高的AQTime违规者是:
std :: _ Uninit_copy>,双*,std :: allocator> 3.65秒
和115731点击
std :: _ Construct 1.69秒和46450637命中
std :: _ Vector_const_iterator> :: operator
!= 1.66秒和46566395命中,依此类推...
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 ==
每个被调用超过4600万次。
我显然做错了所有这些对象的创建。谁能看到我的错误?
最佳答案
这是因为您要按值传递函数参数。每次按值传递std::vector
时,都必须复制向量的完整副本。
更改这些:
double SpecRatio::VValue(vector<double>V, int Index) {
double SpecRatio::RatioPoint(vector<double>Num, vector<double>Denom...
至:
double SpecRatio::VValue(const vector<double> &V, int Index)
double SpecRatio::RatioPoint(const vector<double> &Num, const vector<double> &Denom...
因为为了您的使用,您实际上并不需要制作这些向量的单独副本。