Vector push back slows program...

Hi all,

I'm fairly new to programming and I'm writing a program which has around 6 variables that change at each time step, and at each time step I append the variable to the end of it's associated vector (using push_back). However when I run the program it gets really slow - it starts off around 10 time steps per second, and then slows down to slower than 1 time step every 2 seconds. The only reason this happens is because of the appending to a vector - when I remove these functions the program stays at the original speed. I have also used reserve(4000) to reserve enough space for each vector at the start of the program.

Does anyone have any ideas about why it would slow down so much? One idea I have is that some of the variables are in different objects, so the push_back refers to the variable through a pointer - would this be slow?

Any ideas appreciated,
J.
Do you know what happens when a vector is resized? If you know in advance how large the vector's going to be, you can set the size when you start.

If you really don't know the size up front, and can't store pointers to objects, then maybe you can use linked list instead.
I do know the size at the start - but I used reserve( ) to reserve the size - is resize better?
So I was actually wrong about this - it wasn't actually appending vectors that slowed the program down, I think it was because I was adding 2 floats from 2 different objects that slowed it down. I don't have any idea why this would be slower, but when I added them into a variable, and then just passed that variable to the push_back( ) command it went faster...
Topic archived. No new replies allowed.