What is the runtime complexity of a switch statement?

It is at worst O(n). Sometimes (and this is language and compiler dependent), it translates to a jump table lookup (for “nice” switches that don’t have too large a case range). Then that is O(1).

If the compiler wants to be funky, I can think of ways that the complexity can be implemented to be anything in between (e.g. perform binary search on the cases, for logn). But in practice, you’re going to get eiher linear time or constant time.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)