Arrays or vectors, which are faster?

Started by
11 comments, last by Amnesty 18 years, 9 months ago
I currently have a vector of game objects that I looping through and processing and I was wondering if using a simple array would be quicker?
Gary.Goodbye, and thanks for all the fish.
Advertisement
Arrays are faster but they have a fixed size. Vectors were created to allow you to be able to resize your array but that comes at a processing cost.
What we do in life... Echoes in eternity
Vectors and arrays have EXACTLY the same speed for iteration.

Or maybe one's a little faster. Or a lot faster. Or maybe the other one is. Why not try it out and see? Making a quick test would take three minutes.
A vector is a thin wrapper around a dynamically allocated array. With optimization turned on, access times should be precisely identical. The only thing arrays have in their favour is that it is faster to create/destroy an POD array on the stack as all it involves is a stack pointer adjustment.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Bummer...I like using the Template Library..oh well. I guess that I shoudl use arrays then if I have a fixed number of objects that I need to process.
Gary.Goodbye, and thanks for all the fish.
Depends on what you mean by "processing", with optimizations on std::vector is as fast a C-style dynamic array in some cases it can be faster because of naive use of C-style dynamic arrays (same can be said the other way too), also the standard library containers are parameterized by allocator type so you can use custom memory models/schemes with it.

How-ever If you know the size of the array at compile time at its not going to change you may aswell use a statically allocated array.
Quote:Original post by garyfletcher
Bummer...I like using the Template Library..oh well. I guess that I shoudl use arrays then if I have a fixed number of objects that I need to process.


If your amount of game objects are likely going to increase I would advise sticking with the vector--besides, iterating through your object list is not likely going to be a major bottleneck for your game.
Vectors are more accessable and can change their size use them for your game objects and you'll thank yourself.
-Cory
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
This post begs the old saying "Premature optimization is the root of all evil." Just use which ever you prefer, it's extremely unlikely using vectors instead of arrays will be the main bottleneck in your game; in fact, unless you're making Doom 4, it's doubtful you'll have any bottlenecks for your purposes.
Quote:Original post by bytecoder
This post begs the old saying "Premature optimization is the root of all evil."



Quote:Originally by Len Lattanzi
Belated pessimization is the leaf of no good.


If you know the size of an array at compile time and its not going to change, using a C-style dynamic array or std::vector is pretty much unnecessary.

This topic is closed to new replies.

Advertisement