我有一个std :: map性能问题。在我的C++项目中,我有一个GUIObject
的列表,其中还包括Window
s。我在for
循环绘制的一切,就像这样:std :: map performance C++
unsigned int guiObjectListSize = m_guiObjectList.size();
for(unsigned int i = 0; i < guiObjectListSize; i++)
{
GUIObject* obj = m_guiObjectList[i];
if(obj->getParentId() < 0)
obj->draw();
}
在这种情况下,当我运行一个项目,它工作的顺利进行。我有4个窗口和其他一些部件,如按钮等
但我想借此分别绘制窗口的照顾,所以修改后,我的代码看起来是这样的:
// Draw all objects except windows
unsigned int guiObjectListSize = m_guiObjectList.size();
for(unsigned int i = 0; i < guiObjectListSize; i++)
{
GUIObject* obj = m_guiObjectList[i];
if((obj->getParentId() < 0) && (dynamic_cast<Window*>(obj) == nullptr))
obj->draw(); // GUIManager should only draw objects which don't have parents specified
// And those that aren't instances of Window class
// Rest objects will be drawn by their parents
// But only if that parent is able to draw children (i.e. Window or Layout)
}
// Now draw windows
for(int i = 1; i <= m_windowList.size(); i++)
{
m_windowList[i]->draw(); // m_windowList is a map!
}
所以我创建了一个std::map<int, Window*>
,因为我需要在地图中将Window
的z索引设置为key
。但问题是,当我运行这个代码时,它真的很慢。即使我只有4个窗口(地图大小为4),我可以看到fps率非常低。我不能说一个确切的数字,因为我还没有实施这样的计数器。
谁能告诉我为什么这种方法如此之慢?
你确定它不是dynamic_cast这是慢吗? – Agentlien
避免dynamic_cast,特别是在循环中 – codablank1
@Agentlien是的,我试图删除if语句中的dynamic_cast条件并离开地图内容绘图(因此绘制了多于2个的窗口,但是这个数字不是很多),但仍然非常缓慢。 –