我被这段代码严重困扰了,我运行了gprof,发现程序花费了运行约8000次的contains()函数的40%的时间。程序本身总共需要15秒才能运行。有谁知道为什么需要这么长时间,或者还有什么可能?为什么这段代码运行得如此缓慢?
// Check a list to see if it contains a tile object at x,y
bool contains(std::list<struct Tile>* list, int x, int y){
std::list<struct Tile>::iterator it;
for(it = list->begin(); it != list->end(); it++){
if((*it).x == x && (*it).y == y){
return true;
}
}
return false;
}
列表对于搜索无效。为什么不使用矢量? –
那么,你的名单多久了?线性搜索并不是特别有效(但它与您在链接列表中获得的效果一样好)。 –
放下'struct',只是'Tile'。它更干净。 –