data structures

Started by
7 comments, last by gotG 22 years ago
How efficient is the STL compared to just a plain array on the heap??? i had problems keeping track of all the objects on the heap due to large amount of data structures in a 3d engine to keep track of, and deleting all those pointers gets to be a pain in the A** i find the vector to be easier to use cause the underlying code does the cleanup work(even though the hidden array is still on the heap). I know that when the vectors'' underlying array is full it gets expensive to allocate a larger array, but other then that how does it compare to a array on the heap. and if i made a simpler version of the vector without the complexities of the stl one, but still able to do the things i need done what that be better????
Buckle your seatbelt Dorthy, cause Kansas is going bye bye!!!
Advertisement
I''m confused. You could do two things:

1. Be lazy and use the STL vector
2. Keep trck of your own code (know what pointers you create, etc.)
stl vector is pretty dang fast. It is generally just as fast as doing dynamic arrays by oneself. Implementing a simpler version of a vector probably wouldn''t gain you anything at all.
quote:Original post by gotG
How efficient is the STL compared to just a plain array on the heap???
i had problems keeping track of all the objects on the heap due to large amount of data structures in a 3d engine to keep track of, and deleting all those pointers gets to be a pain in the A**
i find the vector to be easier to use cause the underlying code does the cleanup work(even though the hidden array is still on the heap). I know that when the vectors' underlying array is full it gets expensive to allocate a larger array, but other then that how does it compare to a array on the heap.

and if i made a simpler version of the vector without the complexities of the stl one, but still able to do the things i need done what that be better????


well it is part of what a vector is, that when you try to put stuff into a vector, which is using all of it's array, that it starts getting slow. This is because vectors are just done using normal arrays. The trick is, that the vector guesses how big the array needs to be. You try to put stuff outside the vector, it has to make a bigger array, copy the old array into that, then do what you want --- slow! But it's very fast for random access.

if you don't want random access of the vector (ie, access element 3, then 5, then 2, then 7 etc.), use a linked list - much faster.

Using your own code instead of STL? well STL is quite fast, but doing it yourself is a good way to learn how it all works. But if you aren't that interested in how it all works, use STL.



All done with Cash:

www.venini.co.uk/arthursdeparture/

[edited by - deanolium on April 19, 2002 8:46:11 PM]
All done with Cash:www.venini.co.uk/arthursdeparture/
quote:Original post by gotG
I know that when the vectors'' underlying array is full it gets expensive to allocate a larger array, but other then that how does it compare to a array on the heap.

Given that you''d have to do the reallocation and copy over yourself if you didn''t use it, there is no reason not to use std::vector.

quote:
and if i made a simpler version of the vector without the complexities of the stl one, but still able to do the things i need done what that be better?

No.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
when i say implenmenting a simpler vector, i was thinking something like a vector without things like iterators and such, i just need the bare structure with the ability to
1.random access
2.resize
Buckle your seatbelt Dorthy, cause Kansas is going bye bye!!!

If you use the reserve() member function, vector will set aside as much memory as you want for the vector to expand into without having to resize. If that helps with the speed issue or anything. From what I''ve heard, the stl data structures are very fast, and you won''t notice much overhead. Do a search, I believe that this question has been answered several times before.
Some people think that a big class with many many members is slower than a smaller class. This is not true. It's just like any other C function. If you do this (in C++):
myClass::put(int x){    data = x;}  


...then this behaves exactly the same as this (in C):
put(int x, myStruct * this){    this->data = x;}  


The difference is entirely semantic; you type different things, but the generated code is the same.

As we all know, a C function that isn't called won't slow down your program (in general; I know large executables cause paging problems). The same is true of a C++ class' member. There is one exception, however: a virtual member function requires the storage of a function pointer, by which the function is called.

Anyway, I know this was slightly off-topic, but your comments implied that you shared many common misconceptions, so I figured I'd clear them up.

Happy coding!

[edited by - TerranFury on April 22, 2002 5:00:41 PM]
gotG, next time you ask a question like that, don''t forget that STL != vector. So, you have to specify that you''re talking about STL''s vector class (you can call it std::vector to eliminate the need to specify that it''s STL''s vector).

This topic is closed to new replies.

Advertisement