Standard Template Library

Get answers to all your basic programming questions. No Ogre questions, please!
User avatar
runfrodorun
Gremlin
Posts: 154
Joined: Fri Jun 24, 2011 3:50 am
Location: 192.168.0.1
x 3

Standard Template Library

Post by runfrodorun »

Originally I was a developer for embedded systems, and now I work on games. When I was working with these systems, like the NI cRio, I learned that using Standard Template Library too much was a drain on precious CPU (not huge, but enough makes you notice). On the other hand, the flexibility that it gives me implementation-wise is outstanding. My question what is the real processor footprint of standard template library std::vector, and std::map, etc.? I get nervous when I include them in game code, but they really work magic.
Sass not tolerated
LAY DOWN THE LAW!
When I don't understand a problem, I punch it in the face and try again.
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56

Re: Standard Template Library

Post by Klaim »

See: http://gamedev.stackexchange.com/questi ... yea-or-nay

Basically, fears of STL is misplaced...today at least. A lot of those fears were because of old C++ compilers being far from the standard and also lack of good implementations on embedded devices.\

If you understand well the containers, in particular the vector that is the most useful, then you should be ok with using the STL.

I've shipped (successful) Nintendo DS games using only STL (because we didn't have any base of code before)
Valentin Perrelle
Halfling
Posts: 54
Joined: Thu Sep 15, 2011 4:14 pm
x 2

Re: Standard Template Library

Post by Valentin Perrelle »

Basically, a vector is just an array. With a compiler which have basic optimizations (inlining, constant folding and basic invariant extraction are enough) the compiled code for vectors will just look like you would have wrote yourself with an array. When you write a classical loop with a vector::iterator, it will be compiled the same as a classical for loop from 0 to size-1. (And will be the same if you wrote it++ or ++it) When you use random access to the vector, it will be the same as using the [] operator on your array. And so on. Of course, resizing vectors will have some cost, but the problem would be the same with an array.

Of course, using more complex datatypes may have an impact on your performances. But this is a well understood theoritical problem : choosing the right datatype for the right usage.
AtlasC1
Gnoblar
Posts: 13
Joined: Sun Mar 18, 2012 6:36 am

Re: Standard Template Library

Post by AtlasC1 »

STL containers are far more optimal than what the average programmer would be able to come up with. Keep in mind, STL containers have been tried, tested, and optimized over the years by hundreds of thousands of programmers. Keep in mind, you should know the differences between arrays, queues, lists, vectors, maps, etc., because there is most definitely an optimal choice, depending on how you will be using it (i.e., will you be frequently adding and removing elements, or will it be fairly static after creation? Do you need to be able to access elements by their index, or will you just be iterating over all of the elements? Do you need contiguous memory allocation?).

That said, if you're doing low-level programming, and you know 100% exactly what you need and don't need, and STL doesn't fit your needs, then it may be worth looking into your own implementation.

-atlas