我的代码的这一部分旨在接受一个不规则形状的
我认为问题可能出在新手式的
请注意,
Tile
对象轮廓,并循环创建一个环,每次扩展环以包含刚刚超出前一个环的那些瓷砖,并降低环中瓷砖的高度值(这有意义吗?)。 然而,我发现每次循环都会出现巨大的性能下降,比之前的循环要慢得多。 为什么会这样呢?我认为问题可能出在新手式的
oldEdge = theEdge;
和类似的行(两者都是向量,我将一个赋给了另一个)。 但即便如此,我也不明白为什么会有如此巨大的性能下降。 也许我正在做一些明显愚蠢的事情。 有人能指点一下吗?请注意,
oldEdge
、theEdge
和newEdge
都是vector<Tile*>
。int decrease = 1;
while(decrease < 10)
{
cout << "Trying to smooth!\n";
//First, modify the new edge.
int newHeight = 70 - decrease;
cout << "Height at: " << newHeight << endl;
for(int i = 0; i < newEdge.size(); ++i)
{
newEdge[i]->SetHeight(newHeight);
}
//Increment decrease.
decrease += 1;
//Set the oldEdge and theEdge variables.
oldEdge = theEdge;
theEdge = newEdge;
newEdge.clear();
//Finally, find the new edge.
cout << "Finding new edge!\n";
for(int i = 0; i < theEdge.size(); ++i)
{
//cout << "Checking a tile's neighbors!\n";
for(int j = 0; j < theEdge[i]->m_AdjacentTiles.size(); ++j)
{
bool valid = true;
//Is this neighbor in theEdge?
//cout << "Is this neighbor in theEdge?\n";
for(int k = 0; k < theEdge.size(); ++k)
{
if(theEdge[i]->m_AdjacentTiles[j] == theEdge[k])
{
valid = false;
break;
}
}
//If not, is it in oldEdge?
if(valid)
{
//cout << "Is this neighbor in oldEdge?\n";
for(int k = 0; k < oldEdge.size(); ++k)
{
if(theEdge[i]->m_AdjacentTiles[j] == oldEdge[k])
{
valid = false;
break;
}
}
}
//If neither, it must be valid for continued expansion.
if(valid)
{
newEdge.push_back(theEdge[i]->m_AdjacentTiles[j]);
}
}
}
}
Tile*
而不是Tile
?你尝试过将赋值更改为swap
吗,因为你无论如何都会丢弃newEdge
? - Konrad RudolphtheEdge[i]->m_AdjacentTiles[j]
可以移出循环。(不要指望编译器会这样做。)此外,我会倒序计数,如for(int k = theEdge.size(); valid && --k >= 0;)
。 - Mike Dunlaveysize()
数字的范围吗?虽然这并不一定影响逻辑,但它可以帮助指导优化方向。 - dolphy