Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: What is faster - lookup in hashmap or switch statement?

I have a code pattern which translates one integer to another. Just like this:

int t(int value) {     switch (value) {         case 1: return const_1;         case 3: return const_2;         case 4: return const_3;         case 8: return const_4;         default: return 0;     } } 

It has about 50 entries currently, maybe later on there will be some more, but probably no more than hundred or two. All the values are predefined, and of course I can order case labels by their values. So the question is, what will be faster - this approach or put this into hash map (I have no access to std::map, so I'm speaking about custom hash map available in my SDK) and perform lookups in that table? Maybe it's a bit of premature optimization, though... But I just need your opinions.

Thanks in advance.

EDIT: My case values are going to be in range from 0 to 0xffff. And regarding the point of better readability of hash map. I'm not sure it really will have better readability, because I still need to populate it with values, so that sheet of constants mapping is still needs to be somewhere in my code.

EDIT-2: Many useful answers were already given, much thanks. I'd like to add some info here. My hash key is integer, and my hash function for integer is basically just one multiplication with integral overflow:

EXPORT_C __NAKED__ unsigned int DefaultHash::Integer(const int& /*aInt*/) { _asm mov edx, [esp+4] _asm mov eax, 9E3779B9h _asm mul dword ptr [edx] _asm ret } 

So it should be quite fast.

like image 866
Haspemulator Avatar asked Jul 28 '11 14:07

Haspemulator


People also ask

Is HashMap faster than a switch?

If you can switch, switch. It will always be as fast as if not faster than a hashmap. For integer values and enum values, it is transformed into an in memory lookup table, which doesn't have the cost of hashing. For a string, it creates the HashMap and uses that to switch.

Which is faster if else or switch in C?

if-else better for boolean values: If-else conditional branches are great for variable conditions that result into a boolean, whereas switch statements are great for fixed data values. Speed: A switch statement might prove to be faster than ifs provided number of cases are good.

Why HashMap is fast?

The reason that HashMap is faster than HashSet is that the HashMap uses the unique keys to access the values. It stores each value with a corresponding key and we can retrieve these values faster using keys during iteration. While HashSet is completely based on objects and therefore retrieval of values is slower.


1 Answers

A switch construct is faster (or at least not slower).

That's mostly because a switch construct gives static data to the compiler, while a runtime structure like a hash map doesn't.

When possible compilers should compile switch constructs into array of code pointers: each item of the array (indexed by your indexes) points to the associated code. At runtime this takes O(1), while a hash map could take more: O(log n) at average case or O(n) at worst case, usually, and anyway a bigger constant number of memory accesses.

like image 105
peoro Avatar answered Oct 12 '22 06:10

peoro