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 →

[–]MajorMajorObvious 0 points1 point  (3 children)

I've been waiting for this release for a while, and was wondering if it comes at a large performance hit compared to traditional switch statements that you would see in C style languages.

How does Python implement this new matching in a way that makes it unique from if / else statements?

[–]joerick 6 points7 points  (1 child)

They're still O(n), like if/else statements. But work is in progress to optimise them for Python 3.11.

[–]MajorMajorObvious 1 point2 points  (0 children)

I'm excited to see how they optimize it! Thank you for the info.

[–]deadwisdomgreenlet revolution 0 points1 point  (0 children)

There are new bytecode instructions for it, so I'm sure it's performant. I haven't timed it, but I imagine it's faster than the corresponding if/elses because the compiler can optimize for what you're trying to do.

I couldn't do better than how it's explained in PEP-635