Search code examples
performancelanguage-agnosticswitch-statement

Is there something faster than a switch statement?


When optimizing for performance, so far I'm led to believe that there is nothing faster than a switch statement. Is this the absolute truth?


Solution

  • There's nothing faster then an optimal algorithm (actually nothing at all is probably the fastest, then an optimal algorithm). Micro-optimizations involving switch vs if are rarely going to yield significant improvements on a modern system. However, in limited circumstances (and on older computers) something like Duff's device were valuable (e.g. circa 1983 computers while doing computer animation on computers with extremely limited numbers of registers).