Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8963311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:19:43+00:00 2026-06-15T16:19:43+00:00

I have a problem with std::map performance. In my C++ project I have a

  • 0

I have a problem with std::map performance. In my C++ project I have a list of GUIObjects which also includes Windows. I draw everything in for loop, like this:

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();                                
}

In this case when I run a project, it works smoothly. I have 4 windows and few other components like buttons etc.

But I would like to take care of drawing windows separately, so after modifications, my code looks like this:

// 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!
}

So I created a std::map<int, Window*>, because I need z-indexes of Windows to be set as keys in a map. But the problem is that when I run this code, it’s really slow. Even though I have only 4 windows (map size is 4), I can see that fps rate is very low. I can’t say an exact number, because I don’t have such counter implemented yet.

Could anyone tell me why this approach is so slow?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:19:44+00:00Added an answer on June 15, 2026 at 4:19 pm

    This is what virtual functions are for. Not only do you eliminate the slow dynamic_cast, but you get a more flexible type check.

    // 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)
            obj->drawFirstChance();
    }
    
    // Now draw windows
    for(int i = 1; i <= m_windowList.size(); i++)
    {
        m_windowList[i]->drawSecondChance();
    }
    

    Where drawFirstChance doesn’t do anything for windows and other floating objects.

    The next optimization opportunity is to make the window list a vector and perform z-order sorting only when it changes (assuming windows are created/destroyed/reordered much less often than they are drawn).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with the operator<() method which is required for a std::map.
I have a problem creating a std::map<int, int> from a vector of pointers, called
I have the following cyclic dependency problem I am trying to solve: typedef std::map<int,
I have a std::map< std::string, std::string> which initialized with some API call. When I'm
I have a MAP which is optionally of type std::map and optionally of type
Suppose, I have a very large std::map< unsigned int, Foo > FooDB , which
I have this map which compiles fine in MSVC10 : std::map<std::string, std::ofstream> m_logFiles; But
I have some opaque bytes which I want to use in an std::map ,
I have problem i need to convert from my Array structure to std::vector<int> ...
I have a static std::map<std::string, CreateGUIFunc> in a class that basically holds strings identifying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.