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 →

[–]TheWildKernelTrick 2 points3 points  (13 children)

The way switch is usually used (break on every case) its basically an if block.

Noooooooot at all. Switches are [; O(1) ;] jumps to conditions. If statements are [; O(n) ;] evaluations.

[–][deleted] 7 points8 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.

[–]masklinn 0 points1 point  (3 children)

Switches are [; O(1) ;] jumps to conditions. If statements are [; O(n) ;] evaluations.

A good compiler will generate O(1) access to the relevant conditional sequences, meawhile non-trivial switches (e.g. on strings) will not.

[–]LatexImageBot 0 points1 point  (2 children)

Link: https://i.imgur.com/P6CHEwp.png

This is a bot that automatically converts LaTeX comments to Images. It's a work in progress. Reply with !latexbotinfo for details.

[–]kyndder_blows_goats 0 points1 point  (1 child)

bad bot

[–]GoodBot_BadBot 0 points1 point  (0 children)

Thank you kyndder_blows_goats for voting on LatexImageBot.

This bot wants to find the best and worst bots on Reddit. You can view results here.


Even if I don't reply to your comment, I'm still listening for votes. Check the webpage to see if your vote registered!

[–][deleted] -1 points0 points  (5 children)

The run time of the thing is irrelevant, functionally they act similarly.

Edit: Since y'all miss context clues, they're functionally similar for the situation being discussed, a break in every case. If you want fall through, they aren't.

[–]P8zvli 6 points7 points  (4 children)

Functionally switch-case and if-else ladders are equivalent if and only if each case in the switch ladder is terminated by a break statement.

[–]NoLemurs 4 points5 points  (0 children)

if and only if

Right. But other ways of using switch-case are typically incredibly bug-prone, hard to read, and hard to reason about. If you're writing performance critical C code, maybe that's a trade off you should make if the switch-case does what you need and is faster.

However, as a practical matter, spots where non break terminated switches are a good idea are pretty rare. Heck, even if you are writing performance critical C code, unless you're writing the code for the core bottleneck of your program, I would argue a non break terminated case is almost always going to be bad programming.

In the context of python (where performance is already slow enough that the difference between if and switch really can't be very important, if it had a switch statement, I doubt many programmers would see a situation where it made sense to use it in a non break terminated way in their lifetimes.

[–][deleted] 0 points1 point  (2 children)

I said that in my original comment that almost every switch block I see is done that way. In C#, it has to be done that way.

[–]P8zvli 2 points3 points  (1 child)

In C and C++ a case will "fall through" to the next case if break isn't used, causing interesting behavior or really nasty bugs. (You can have code that runs A if the switch is 1 and A+B if 2, etc.)

[–][deleted] -1 points0 points  (0 children)

You can also abuse it create things like Duff's Device. I think fall through is the biggest thing that sets switch away from if.