我有一個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個的窗口,但是這個數字不是很多),但仍然非常緩慢。 –