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 8277821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:48:22+00:00 2026-06-08T08:48:22+00:00

I had a previous question about a stack overflow error and switch to vectors

  • 0

I had a previous question about a stack overflow error and switch to vectors for my arrays of objects. That question can be referenced here if needed: How to get rid of stack overflow error

My current question is however, how do I speed up the initialization of the vectors. My current method currently takes ~15 seconds. Using arrays instead of vectors it took like a second with a size of arrays small enough that didn’t throw the stack overflow error.

Here is how I am initializing it:

in main.cpp I initialize my dungeon object:

dungeon = Dungeon(0, &textureHandler, MIN_X, MAX_Y);

in my dungeon(…) constructor, I initialize my 5×5 vector of rooms and call loadDungeon:

Dungeon::Dungeon(int dungeonID, TextureHandler* textureHandler, int topLeftX, int topLeftY)
{
    currentRoomRow = 0;
    currentRoomCol = 0;
    for (int r = 0; r < MAX_RM_ROWS; ++r)
    {
        rooms.push_back(vector<Room>());
        for (int c = 0; c < MAX_RM_COLS; ++c)
        {
            rooms[r].push_back(Room());
        }
    }
    loadDungeon(dungeonID, textureHandler, topLeftX, topLeftY);
}

my Room constructor populates my 30×50 vector of cells (so I can set them up in the loadDungeon function):

Room::Room() 
{ 
    for (int r = 0; r < MAX_ROWS; ++r)
    {
        cells.push_back(vector<Cell>());
        for (int c = 0; c < MAX_COLS; ++c)
        {
            cells[r].push_back(Cell());
        }
    }
}

My default cell constructor is simple and isn’t doing much but I’ll post it anyway:

Cell::Cell() 
{ 
    x = 0;
    y = 0;
    width = 16;
    height = 16;
    solid = false;
    texCoords.push_back(0);
    texCoords.push_back(0);
    texCoords.push_back(1);
    texCoords.push_back(0);
    texCoords.push_back(1);
    texCoords.push_back(1);
    texCoords.push_back(0);
    texCoords.push_back(1);
}

And lastly my loadDungeon() function will set up the cells. Eventually this will read from a file and load the cells up but for now I would like to optimize this a bit if possible.

void Dungeon::loadDungeon(int dungeonID, TextureHandler* textureHandler, int topLeftX, int topLeftY)
{
    int startX = topLeftX + (textureHandler->getSpriteWidth()/2);
    int startY = topLeftY - (textureHandler->getSpriteHeight()/2);
    int xOffset = 0;
    int yOffset = 0;
    for (int r = 0; r < MAX_RM_ROWS; ++r)
    {
        for (int c = 0; c < MAX_RM_COLS; ++c)
        {
            for (int cellRow = 0; cellRow < rooms[r][c].getMaxRows(); ++cellRow)
            {
                xOffset = 0;
                for (int cellCol = 0; cellCol < rooms[r][c].getMaxCols(); ++cellCol)
                {
                    rooms[r][c].setupCell(cellRow, cellCol, startX + xOffset, startY - yOffset, textureHandler->getSpriteWidth(), textureHandler->getSpriteHeight(), false, textureHandler->getSpriteTexCoords("grass"));
                    xOffset += textureHandler->getSpriteWidth();
                }
                yOffset += textureHandler->getSpriteHeight();
            }
        }
    }

    currentDungeon = dungeonID;
    currentRoomRow = 0;
    currentRoomCol = 0;
}

So how can I speed this up so it doesn’t take ~15 seconds to load up every time. I feel like it shouldn’t take 15 seconds to load a simple 2D game.

SOLUTION
Well my solution was to use std::vector::reserve call (rooms.reserve in my code and it ended up working well. I changed my function Dungeon::loadDungeon to Dungeon::loadDefaultDungeon because it now loads off a save file.

Anyway here is the code (I got it down to about 4-5 seconds from ~15+ seconds in debug mode):

Dungeon::Dungeon() 
{ 
    rooms.reserve(MAX_RM_ROWS * MAX_RM_COLS);
    currentDungeon = 0;
    currentRoomRow = 0;
    currentRoomCol = 0;
}
void Dungeon::loadDefaultDungeon(TextureHandler* textureHandler, int topLeftX, int topLeftY)
{
    int startX = topLeftX + (textureHandler->getSpriteWidth()/2);
    int startY = topLeftY - (textureHandler->getSpriteHeight()/2);
    int xOffset = 0;
    int yOffset = 0;
    cerr << "Loading default dungeon..." << endl;
    for (int roomRow = 0; roomRow < MAX_RM_ROWS; ++roomRow)
    {
        for (int roomCol = 0; roomCol < MAX_RM_COLS; ++roomCol)
        {
            rooms.push_back(Room());
            int curRoom = roomRow * MAX_RM_COLS + roomCol;
            for (int cellRow = 0; cellRow < rooms[curRoom].getMaxRows(); ++cellRow)
            {
                for (int cellCol = 0; cellCol < rooms[curRoom].getMaxCols(); ++cellCol)
                {
                    rooms[curRoom].setupCell(cellRow, cellCol, startX + xOffset, startY - yOffset, textureHandler->getSpriteWidth(), textureHandler->getSpriteHeight(), false, textureHandler->getSpriteTexCoords("default"), "default");
                    xOffset += textureHandler->getSpriteWidth();
                }
                yOffset += textureHandler->getSpriteHeight();
                xOffset = 0;
            }
            cerr << "     room " << curRoom << " complete" << endl;
        }
    }
    cerr << "default dungeon loaded" << endl;
}

Room::Room()
{ 
    cells.reserve(MAX_ROWS * MAX_COLS);
    for (int r = 0; r < MAX_ROWS; ++r)
    {
        for (int c = 0; c < MAX_COLS; ++c)
        {
            cells.push_back(Cell());
        }
    }
}
void Room::setupCell(int row, int col, float x, float y, float width, float height, bool solid, /*std::array<float, 8>*/ vector<float> texCoords, string texName)
{
    cells[row * MAX_COLS + col].setup(x, y, width, height, solid, texCoords, texName);
}

void Cell::setup(float x, float y, float width, float height, bool solid, /*std::array<float,8>*/ vector<float> t, string texName)
{
    this->x = x;
    this->y = y;
    this->width = width;
    this->height = height;
    this->solid = solid;
    for (int i = 0; i < t.size(); ++i)
        this->texCoords.push_back(t[i]);
    this->texName = texName;
}
  • 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-08T08:48:23+00:00Added an answer on June 8, 2026 at 8:48 am

    It seems wasteful to have so many dynamic allocations. You can get away with one single allocation by flattening out your vector and accessing it in strides:

    std::vector<Room> rooms;
    
    rooms.resize(MAX_RM_ROWS * MAX_RM_COLS);
    
    for (unsigned int i = 0; i != MAX_RM_ROWS; ++i)
    { 
        for (unsigned int j = 0; j != MAX_RM_COLS; ++j)
        {
            Room & r = rooms[i * MAX_RM_COLS + j];
            // use `r`       ^^^^^^^^^^^^^^^^^^^-----<< strides!
        }
    }
    

    Note how resize is performed exactly once, incurring only one single allocation, as well as default-constructing each element. If you’d rather construct each element specifically, use rooms.reserve(MAX_RM_ROWS * MAX_RM_COLS); instead and populate the vector in the loop.

    You may also wish to profile with rows and columns swapped and see which is faster.

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

Sidebar

Related Questions

In a previous question on Stack Overflow , I had run into an issue
This is a follow up to a previous question that I had before about
In previous question of mine, someone had meantioned that using Semaphores were expensive in
I'm trying to implement this algorithm description from a previous question I had here
In my previous question on this portal, I had asked about some insight about
This question is a followup to a previous question I had about discovering unused
This is a follow-up to a previous question I had about interfaces. I received
I thought I had understanding of this in my previous question about linked lists,
I asked about pipes in a previous question, got that working perfectly. However I
In my previous question I had a problem with sending parameters over the command

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.