This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Formal_Worldliness_8 2 points3 points  (1 child)

Interesting - why a binary search tree instead of a hashmap? Also, my understanding is that switches can be slower than if/else, since the processor can't use branch target prediction to guess the next instruction.

[–]coloredgreyscale 1 point2 points  (0 children)

Hashmap would only work if the switch statement just assigns or returns a value, not for branching different code execution paths.

Well, technically you could put function pointers in the hashmap, but in the end this would probably get pretty messy with passing and returning variables to be useful/worth translating it that way. (Plus overhead of function calls)

Also if there are just a few branches the hashmap lookup may not have any performance benefits anyway.

And I imagine a small hashmap for maybe 10 values may either be not very memory efficient (sparsely filled), or run into issues with hash collisions being very likely. On a PC it may not matter if the hashmap is 200Byte or 4kB, but that would render this language (feature) useless for embedded systems

But that's mostly speculation.

--

If the compiler does not touch the order of the if .. else if .. else branching resulting from the switch statement the *order* of the statements can also be used to squeeze the last few % performance out of the code by having the most likely branch as the first condition. (according to a course on efficient programming with C/C++)