gpt4 book ai didi

c++ - 需要 std::vector 优化

转载 作者:行者123 更新时间:2023-11-28 00:55:46 27 4
gpt4 key购买 nike

我编写了一个使用 std::vector<double> 的例程相当沉重。它运行得相当慢,AQTime 似乎暗示我正在构建大量的 vector ,但我不确定为什么会这样。对于某些上下文,我的示例运行迭代了 10 次。每次迭代将 3 个约 400 点的 c 数组复制到 vector 中,并创建 3 个新的相同大小的 vector 用于输出。每个输出点可能是来自 2 个输入 vector 的最多 20 个点相加的结果,最坏的情况是 10*400*3*2*20 = 480,000 次取消引用。令人难以置信的是,探查器表明某些 std::方法被调用了 4600 万次。我怀疑我做错了什么!

部分代码:

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 >, 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 ==

每个都被调用超过 4600 万次。

我显然做错了什么导致创建所有这些对象。谁能看到我的错误?

最佳答案

这是因为您正在按值传递函数参数。每次按值传递 std::vector 时,都必须制作 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...

因为对于您的使用,您实际上从来不需要制作这些载体的单独拷贝。

关于c++ - 需要 std::vector 优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11406026/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com