【发布时间】:2016-02-21 17:53:36
【问题描述】:
所以我正在用 c++ 和 opengl 为我的一类人制作一个渲染器。我正在制作一个动画程序以获得额外的功劳,它将在我的渲染器在每一帧中读取它们之前更改文本文件中的值。我的问题是这部分代码写得不够快
while (clock() < time_end)
{
timeStep = clock() + fps * CLOCKS_PER_SEC;
for(int k=0; k < currOps.size(); k++)
{
// increase/decrease each set once for the current timestep
// a case for each operation
int pos = currAxis[k];
if(currOps[k] == "loc")
{
opsFile[8+pos] = patch::to_string(atof(opsFile[8+pos].c_str()) + locScale[pos-1]*timeAdjust);
//edit this value by loc adjust
}
else if(currOps[k] == "rot")
{
opsFile[4+pos] = patch::to_string(atof(opsFile[4+pos].c_str()) + rotScale[pos-1]*timeAdjust);
//edit this value by rot adjust
}
else if(currOps[k] == "scl")
{
opsFile[pos] = patch::to_string(atof(opsFile[pos].c_str()) + sclScale[pos-1]*timeAdjust);
//edit this value by scl adjust
}
}
currFile.close(); //save file and restart so we don't append multiple times
currFile.open(files[location[0]].c_str(), ofstream::out); // so we can write to the file after closing
for(int j=0; j <opsFile.size(); j++)
{
// update the file
currFile << opsFile.at(j);
currFile << "\n";
}
while(clock() < timeStep)
{
//wait for the next time steps
}
}
最后特别是 currFile 操作。如果我取出 currFile 操作,它将以所需的 fps 运行。 FPS 设置为 0.033,因此它的速度为 30 fps。当 fps = 0.1 时,它也会运行得足够快。任何优化都会很棒。如果需要查看我的代码的任何其他部分,请告诉我,我会上传。整个事情大约有170行。 currOps、files 和 opsFile 是字符串的向量 sclScale、rotScale 和 locScale 是双精度的向量 currAxis 是整数的向量
【问题讨论】:
-
另外,opsFile 是一个向量
,大约有 12 行。 -
有趣。为什么需要实时写入文件?
-
currFile << endl;语句不仅会写入换行符,还会将数据刷新到磁盘。您可能只需要前者,而不需要后者。尝试改用currFile << '\n';。 -
您是否已经对您的应用进行了概要分析?真的是文件写入是瓶颈,还是你在“currOps.size()”的循环中进行的所有 ascii->float->ascii 转换?
标签: c++ file optimization