std::map vs. C Arrays: Performance

Mar 16, 2011 at 11:01pm
For some libraries in the game engine I'm just starting to write, I am using std::map<std::string, some_other_type> for collections of types which must later be identified.

Due to the nature of the code, it's easier to use string IDs rather than integers in a standard array: e.g., some_other_type[256], say.

Obviously using the second method + pointer arithmetic is much faster than using std::map to search through the array for a certain string.

Given this is a game engine and resources will need to be accessed quickly, frame by frame, should I be using the second, rather messier method for increased performance?

Thanks in advance for any help.
Mar 16, 2011 at 11:16pm
It's not messy. Not at all. Several years ago, I've programmed very complex games using nothing but arrays. Not to mention that I did it without an object oriented language, where the language I was using didn't even support pointers or dynamic memory (haha Game Maker's GML only has variables, arrays, very basic control structures and scripts/functions) ... Learn how to program this way before you start anything serious. If you understand how to do that, you can discover a lot of tricks that aren't possible without a solid understanding of this. Classes, containers and all of the other goodies that C++ has are very useful, but don't rely on them.
Mar 16, 2011 at 11:55pm
I'm of the opinion that you should design things to be functional, first. If they don't perform to your satisfaction you can optimize them later.

The problem with optimizing first is that you might be doing all this work for nothing. For instance... would the minor performance difference between std::map and an array have any effect on how well the overall game runs?

Unless you're accessing it tons, it probably won't matter. (read: a hundred or so times every frame is practically nothing).


Now that doesn't mean the std::map approach is better. Really, it depends on your design. Maybe the array approach suits you better. It really depends on how you want your program to be arranged.

Basically what I'm saying is... don't sweat the small stuff. Worrying about performance when your program performs well is pointless.
Mar 17, 2011 at 12:06am
Not that I'm changing my mind, but I agree with that as well. It won't make much big of a difference and it makes things a 'bit' easier. Especially if you want to make it user friendly (it is a game engine after all, you're going to need some easy-of-use for other developers). However, I still don't see much need for strings. Often, maps are handy for more complicated purposes. Why is it so bad to use an integer?
Mar 17, 2011 at 12:12am
I tend to use strings for IDs simply because it makes it easier to pair them with a filename (my maps/images/etc are stored in external files, so it's easiest if the filename is the key). The alternative is to store the filename seperately from the ID, or only use numerical IDs for file names ("00000001.map", etc)

But again it all comes down to your design.
Last edited on Mar 17, 2011 at 12:12am
Mar 17, 2011 at 3:08am
Also, if you find the std::map is not performing well, you might find that unordered_map (available on most compilers these days) works much better and only requires changing a header and a typedef.
Mar 17, 2011 at 8:09am
Thanks for all the advice everyone. Yes I'm using strings for the same reason as Disch: so the filename can be used as the ID.

I'll look up the unordered_map, but ultimately it sounds as if I may as well everything mostly it as it is until I actually have a chance to measure its performance in a game situation (which may be some time as I'll have to write the rest of the engine...)
Topic archived. No new replies allowed.