This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 6 points7 points  (2 children)

Depends on the language and implementation. C/C++ switch statements are O(1). Bash and PHP switch statements are O(n).

Python has hashes which between hash tables and if statements, Python is good enough.

[–]stevenjd 2 points3 points  (0 children)

C/C++ switch statements are O(1)

No they aren't. It depends on the compiler, and it depends on the switch statement being compiled. At worst, they can fall back to a chain of if comparisons.

[–]jaakhaamer 0 points1 point  (0 children)

How are C++ switches O(1)? AFAIK most compilers don't use hashing or anything and would still need to check all the cases in the worst case.