Computing a CRC will necessarily be slower than any string comparison, but you can count on matching with log2N before looking for a key (for example, 10 comparisons for 1000 keys), so this depends on the size of your map . CRC can also lead to collisions, so it is error prone (you can detect collisions relatively easily to detect and maybe even process them to get the correct results anyway, but you need to be very careful that everything is correct).
You can try unordered_map<> (possibly called hash_map ) if your C ++ environment provides one thing - it may or may not be faster, but it will not be sorted if you iterate. Hash maps are another tradeoff:
- the time for the hash probably looks like the time for your CRC, but
- after that, they can often search directly for the value, rather than perform a double tree movement in a normal map.
- they predetermine a bit of logic for handling conflicts.
(a dumb point, but if you can continue to use int and they can be contiguous, remember that you can replace the search with an array that is much faster. If integers are not contiguous, t is especially rare, you can use a sparse index, for example an array out of 10,000 short ints, which are indices in 1000 packed records).
Bottom line - if you care about asking, you should implement these alternatives and benchmark to see what really works best with your specific application, and if they really make any tangible difference. Any of them may be the best in certain circumstances, and if you care to seriously compare them, then this clearly means that any of them will do.
source share