Sign in to follow this  
garyfletcher

Arrays or vectors, which are faster?

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Vectors are more accessable and can change their size use them for your game objects and you'll thank yourself.
-Cory

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
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.
It also doesn't hurt. Especially when the original poster has apparently done no work to determine if the performance of vector is having the slightest impact on him or not.

Besides, statements like "the size of this is never going to change" tend to be wrong as the project matures and requirements change.

Share this post


Link to post
Share on other sites
Quote:

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.


Quote:

Just use which ever you prefer, it's extremely unlikely using vectors instead of arrays will be the main bottleneck in your game;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this