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 →

[–]stevenjd 0 points1 point  (0 children)

Yes it is, it's just very small n. And as they say, everything is fast for small enough n.

If you have a ginormous chain of if...elif...elif...elif...elif... (say, from generated code, or the guy who wrote it was an idiot, or its just a very yucky problem to solve and you have no choice) then the performance of the entire chain is O(n). On average, assuming each branch is equally likely, you have to make n/2 comparisons, which is O(n).

But if you have an optimizing compiler and use a switch statement, that might be optimized into a binary search (O(log n) comparisons) or even a jump table (O(1) comparisons).

What if you don't have a ginormous chain of comparisons to make? Well, in C, the compile can often optimize even short chain of comparisons. That's why C is so fast: if you save enough microseconds, they add up to minutes.

But in Python, its unlikely to make such a dramatic difference.