this post was submitted on 06 Sep 2025
473 points (97.2% liked)
Programmer Humor
26188 readers
477 users here now
Welcome to Programmer Humor!
This is a place where you can post jokes, memes, humor, etc. related to programming!
For sharing awful code theres also Programming Horror.
Rules
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Just stupid
If you couldn't write
I suppose you typed this "all the size of a lookup table with none of the speed" abomination manually too.
switch case structures are very efficient in c and c++. They work similarly like an offset into memory. Compute the offset once (any expression in the 'case' lines), then jump. Using primitives directly, like here with chars, is directly the offset. Contrary to if-else branches, where each case must be evaluated first and the CPU has basically no-op cycles in the pipeline until the result of the branch is known. If it fails, it proceeds with the next one, waits again etc.. (Some CPU architectures might have stuff like speculative branch execution, which can speed this up.)
However, code-style wise this is really not elegant and something like your proposal or similar would be much better.
Oh, I didn't know that they were a LUT of jump addresses. Stil, a LUT of values would be more space-efficient and likely faster. Also, what if the values are big and sparse, e.g.
...does the compiler make it into an if-else-like machine code instead?
Yes, I did type it out manually (not really, I just copy pasted it and changed the according values)