3 ms·
> Having 100 cases means we could potentially check 100 cases before selecting one. case statements are O(n), where n is the number of case/switch statements po
by xboxnolifes 4y ago
> Having 100 cases means we could potentially check 100 cases before selecting one. case statements are O(n), where n is the number of case/switch statements possible.
I was under the impression that most (all?) C compilers would optimize this use-case into a jump-table, turning this case statement usage into an O(1) operation. Or is this a C++ feature that I'm thinking of?
- Gibbon1 4y agoIf you want fun see what a C compiler does to your switch statement with -Os My opinion is the big advantage of dispatch tables in C is decoupling modules from each other. Tip: Taking things further: variadic function pointers.
- JonChesterfield 4y agoQuite a lot of effort goes into compiling switch statements. Jump tables and binary search are likely, as is a mixture of the two based on distribution of cases. I haven't seen computed goto but wouldn't be surprised if some compilers have that as a third option.