Skip to main content
GameDev.net gamedev.net
🔒 Locked

stl vector vs array

Started by necromancer_df Sep 6, 2002 at 1:02 AM 21 replies 12.6k views
Original Post
necromancer_df
necromancer_df
does anyone know how the access times of an stl vector compare to a n array? i haven't really been able to find any useful comparisons. the best i've been able to come with is that the speed difference is "not noticable". so which do you think is better to store all of my game objects in, an array or a vector, or something totally different? edit: sorry if this comes up a lot, the search page has been down for a few hours [edited by - necromancer_df on September 6, 2002 2:03:42 AM]
Stoffel
Stoffel
Go to your local library or book store and pick up "Effective STL" by Scott Meyers. He lays down in the first tip or two when to use each container.

Vectors are just as fast as dynamic arrays (in fact, they are dynamic arrays), so that should answer your question.
Zipster
Zipster
I like to think of them as "sophisticated arrays"... like Stoffel said, they can be dynamic, plus you can perform special operations such as random insertions and deletions, granted you get the speed hits, since this still behaves like an array mind you. But it's a lot easier than implementing your own dynamic array. Three cheers for library code written by someone else!

[edited by - Zipster on September 6, 2002 2:19:28 AM]
necromancer_df
necromancer_df
thx dude, i''ll check that book later. so would you suggest that i use vectors for everything rather than standard arrays?
Sneftel
Sneftel
When in doubt, use a vector. There are certain cases where arrays are better, tho.

The main problem with vectors is that there''s a certain amount of time taken to create/destroy them. Static arrays, however, are allocated on the stack, and take pretty much no time to allocate.

So if you had a function, say, that used as temporary storage an array of 5 integers, you''d be better off using an array than a vector.



Don''t listen to me. I''ve had too much coffee.
jwalker
jwalker
you can set the capacity of the vector at the start to increase the speed if you know how many elements are going in.

and memory in a vector is linear, so you can always do this abc.begin() + 5 to access the 6th element.


civguy
civguy
quote:
Original post by Stoffel
Go to your local library or book store and pick up "Effective STL" by Scott Meyers.
If I have already read Bjarne Stroustrup''s "The C++ Programming Language", should I also read that one? Stroustrup''s book had a fair share of STL too.
SabreMan
SabreMan
quote:
Original post by jwalker
you can set the capacity of the vector at the start to increase the speed if you know how many elements are going in.

The same concerns apply to arrays. Basically, the C++ concept of an array is C legacy - something that "they" thought was a good idea in 1976. The problems with arrays are well understood, so a vector should be regarded as the 21st century idea of what an array should be. To put a rule of thumb on it: always prefer vectors to arrays unless you have a proven specific need for an array. For many (probably most) applications, you should not use arrays at all.

quote:
Original post by civguy
If I have already read Bjarne Stroustrup''s "The C++ Programming Language", should I also read that one?

Yes, and probably about 20 others aswell.
civguy
civguy
quote:
Original post by SabreMan
Yes, and probably about 20 others aswell.
Come on, you can''t be serious... Can you? STL isn''t that hard of a subject, at least when it comes down to using it and not studying how it''s made.

SabreMan
SabreMan
quote:
Original post by civguy
Come on, you can''t be serious... Can you? STL isn''t that hard of a subject, at least when it comes down to using it and not studying how it''s made.

Semi-serious. The point I''m really making is that there''s always more books to read, and more to learn about programming, whether it be C++, STL, or any other topic. Reading and learning should continue throughout a s/w developers career, so when you ask ''If I have already read Bjarne Stroustrup''s "The C++ Programming Language", should I also read that one?'', the answer is, of course, yes!

On a slightly more serious note, Effective STL will give you an insight into how to put the STL to good use, and to avoid various pitfalls of the library. Many of the tips and tricks are not explained in other texts. If you''re a keen reader, its well worth a look.
civguy
civguy
Ok, I think I''ll try to obtain it (gotta check library first). What I really wanted to know with my original question was if Effective STL only covered what''s taught in Bjarne''s book. For example, it wouldn''t be of any use for me to read a book called "C++ basics" now . But it seems like E STL covers some more
DrPizza
DrPizza
The characteristics of a deque also make it worth considering.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
Stoffel
Stoffel
quote:
Original post by civguy

If I have already read Bjarne Stroustrup''s "The C++ Programming Language", should I also read that one? Stroustrup''s book had a fair share of STL too.
Absolutely. Scott''s book is basically: "So you''ve been using STL--here''s what you''re probably doing wrong and how to do it the right way." Meyers'' books on C++ and STL are some of the best intermediate programming books I''ve read.
null_pointer
null_pointer
quote:
The characteristics of a deque also make it worth considering.


A bit later than I expected, but still entirely predictable.
DrPizza
DrPizza
Meyers'' book is not a beginners'' guide, and is much better for it. It gave me a number of "doh!" moments. Which I *think* is a good thing.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
dede
dede
Remember, STL is the Standard that is not the Standard.

Really.

All STL implementation do it alittle differently, so I doubt you will find an example that shows the correct costs, etc...,Since it will vary depending on which compiler and which processor you have...

As the standard goes, vectors should be layed out in memory just like an array, i.e.

array + 5 -> array[5]
vector + 5 -> vector[5]

So the access time will be the same...
And if its not like that, it should create an exception and reorginize itself so the next access is....
at least according to the standard










daerid
daerid
quote:
Original post by dede
As the standard goes, vectors should be layed out in memory just like an array, i.e.



Should, yes. But by no means is it required.
daerid@gmail.com
Deyja
Deyja
The rules for choosing are very simple...

If it changes size, use a vector.
If it doesn''t change size, and you know how big it will be at compile-time, use an array.
If it doesn''t change size, and you do not know how big it will be at compile-time, pick either or.

Vectors have slower access - no matter how good the implementation is. An array access is an addition and a pointer de-reference. A vector access is atleast that plus a function call.

And it seems MSVC++6 chokes on a vector of vectors. :/ vector> myvector just dies horribly.
DrPizza
DrPizza
quote:
A vector access is atleast that plus a function call.

My compiler would beg to differ.

I assume yours has heard of inline functions? If not, why not?

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
SabreMan
SabreMan
quote:
Original post by Deyja
The rules for choosing are very simple...

I prefer a simpler rule: unless you specifically need an array, use a vector. The avoidance of arrays can be justified on the grounds of general array bogosity, such as passing to a function, finding out the size, and returning from a function.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.