Vector push_back incredibly slow

Kinru

So I'm in the process of converting Java code that I wrote into C++ code for performance reasons as well as the intention of using CUDA to parallelize some of the stuff down the road. However, the first thing I wanted to do was a straight conversion and just have it running in C++ with the same code as was in java.

The issue I'm running into is that the loop down below is literally taking minutes to finish in C++ while it took barely any time at all in Java. The only difference being that I am using a vector in C++ and an ArrayList in Java.

I am also reserving the proper size for the neighbors vector when I initially create the cells vector. The purpose of this code is to create a uniform grid of cells in a 3d cube and to store the neighbors of each cell inside of the cell itself for convenience later.

I am using Visual Studio 2013 in case that matters (for C++) and Eclipse for java.

I feel like I am definitely missing something simple here as such a slowdown seems crazy, but when I comment out the push_back, the code executes basically instantly.

w, h, and d are all 20. cells is a vector of Cell structs (seen below).

for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
        for (int k = 0; k < d; k++) {
            for (int x = -1; x < 2; x++) {
                for (int y = -1; y < 2; y++) {
                    for (int z = -1; z < 2; z++) {
                        if (i + x >= 0 && i + x < w && j + y >= 0 && j + y < h && k + z >= 0 && k + z < d) {
                            cells[i][j][k].addNeighbor(cells[i + x][j + y][k + z]);
                        }
                    }
                }
            }
        }
    }
}

Defined in a different file:

struct Cell {
    std::vector<Particle> particles;
    std::vector<Cell> neighbors;
    int b = 0;

    void addParticle(Particle &p) {
        particles.push_back(p);
    }

    void addNeighbor(Cell &c) {
        neighbors.push_back(c);
    }
};
Rufflewind

In C++, standard containers such as vector store their elements by value, not by reference (as would be in Java). This means your loops are creating cells that don't just refer to other cells, but contain them. You end up creating a huge forest of nested of vectors that contain vectors, that themselves also contain vectors, and so on (up to about 20 levels in depth!).

What you probably want to do is to store a pointer to the adjacent cells instead:

struct Cell {
    ...

    std::vector<Cell*> neighbors;

    ...

    void addNeighbor(Cell &c) {
        neighbors.push_back(&c);
    }
};

This allows the cells to store weak references to each other.

Keep in mind that C++ doesn't have a garbage collector, nor does it do much safety checking, so it's entirely your responsibility to make sure that the cells are deallocated when they are no longer needed and that pointers are not dereferenced when the cells are gone.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Gradle build incredibly slow

From Dev

while read incredibly slow

From Dev

SSE incredibly slow

From Dev

vector push_back in STL?

From Dev

Vector and push_back() behavior

From Dev

Unable to push_back to vector

From Dev

vector push_back zero into an empty vector

From Dev

push_back a vector of vectors into a vector

From Dev

POS-Tagger is incredibly slow

From Java

Insert or push_back to end of a std::vector?

From Dev

Implementatnion of std::vector::push_back in MSVC

From Dev

vector::push_back and std::move

From Dev

Why are there two overloads for vector::push_back?

From Dev

string Vector push_back failing in class

From Dev

garbage values for vector push_back

From Dev

push_back new element to vector

From Dev

Implementatnion of std::vector::push_back in MSVC

From Dev

garbage values for vector push_back

From Dev

Vector push_back Array of doubles

From Dev

Push_back variadic function parameters into a vector?

From Dev

c++ push_back() a struct into a vector

From Dev

vector of reference wrapper, push_back failure?

From Dev

push_back a pointer to a vector of pointers

From Dev

How to use vector iterators when using vector<>::push_back()

From Dev

Calling std::vector::push_back() is changing previous elements in vector?

From Dev

Calling std::vector::push_back() is changing previous elements in vector?

From Dev

How to use vector iterators when using vector<>::push_back()

From Dev

C++ Vector: push_back Objects vs push_back Pointers performance

From Dev

vector push_back causes assertion error but list push_back works