4 ms·
Wouldn't the compiler optimize this to an array lookup? static const char* countries[257]; Or even: static const char countrycodes[514]; return c
by crazypython 5y ago
Wouldn't the compiler optimize this to an array lookup?
static const char* countries[257];
Or even:
static const char countrycodes[514];
return countrycodes[i * 2];
- dahfizz 5y agoDoesn't look like it. Each case is a compare and jump: https://godbolt.org/z/KWYEW3d9s https://godbolt.org/z/KWYEW3d9s
- ChrisSD 5y agoTurn the gcc version up to 11.
- nitwit005 5y agoPlaying around a bit, it does seem that clang will do so: https://godbolt.org/z/1czW91bMM https://godbolt.org/z/1czW91bMM A presentation on algorithm improvements popped up when looking into it (2015): https://llvm.org/devmtg/2015-10/slides/Wennborg-SwitchLowering.pdf https://llvm.org/devmtg/2015-10/slides/Wennborg-SwitchLoweri...
- deleted 5y ago[deleted]
- jfrunyon 5y agostatic const char countrycodes[514]; return countrycodes[i * 2]; Err... each of the strings is 3 bytes long.