Posted 27 February 2014 - 10:28 AM
std::map is a tree sorted by the key value. It takes O(log N) time to find the value. It makes comparisons as it walks down the tree in a binary search.
std::unordered_map (c++11) is a hash table ordered by key value. It computes a hash of they key, then looks up the slot directly in a hash table.
And finally, storing them sorted in a std::vector or a non-dynamic array allows you to do a binary search if it is sorted, or a linear search if it is not.
So to figure out fastest, you need to figure out how lookups are going to happen. How many items (N) are you storing? A linear search takes N/2 comparisons on average, but benefits from cache effects as you run through the data. The cost for doing 1 may be the same cost as doing 16 or 32 compares thanks to the cache helping out a linear accessing pattern. A binary search takes log N comparisons but they don't get a cache bonus as they are not linear. Finally, a hash table has a near-constant time cost, but it must go through the effort of computing a hash, then looking in the bucket that has the hash; if there are multiple items in the bucket it must search through those few items.
Which of those is fastest depends on the cost to compare the keys (a single machine word is much faster than a string based comparison) and on the cache friendliness of the data (a machine word is smaller than a string, and a string can possibly require more memory lookups) and finally on the cost to compute a hash.
I will add my vote to using a machine word size for the key as well. It is the preferred efficient size for processing on the machine.
Incidentally, many games use a layered approach to resource lookup so that it happens as infrequently as possible.. They have a long name, such as a file name or a resource key or some other value that is used outside the system. When a request for the named object is made the object gets looked up (hash table) to see if it is in the resource list already. If no, the item is added to the resource list and the key and list index are added to the hash table; the program now has the resource index and can use that for the rest of the running of the application. The resource list is not actually the data, but a proxy to the data that can be loaded and unloaded as needed based on game-specific factors. The net result is that the game gets either a fixed index (or even a fixed pointer) in the resource list so hopefully lookup needs to happen only once, ever.
As Scourge mentioned, the number of items stored is important.