15 ms·
I'm also dubious on the claim the switch statement is O(n). It might be in a pathological worst case, but you can pretty much bet the compiler is going to trans
by hermitdev 4y ago
I'm also dubious on the claim the switch statement is O(n). It might be in a pathological worst case, but you can pretty much bet the compiler is going to transform it into a jump table or other optimized execution (maybe a computed jump). Especially when the cases are contiguous like this...
I agree that a benchmark is warranted, or at least a comparison of the generated assembly (at different optimization levels).
- nynx 4y agoAlso, O(n) doesn’t mean much when the CPU can execute hundreds of checks in a few tens of cycles.
- dzaima 4y agoNot for jumps - modern CPUs still (usually) have limit of one taken branch per cycle, or 1 to 2 untaken branches. And usually branch prediction will be the limiting factor anyway, for which a single branch is gonna be faster than many.