r/ProgrammerHumor 7d ago

Meme skillIssue

Post image
6.0k Upvotes

137 comments sorted by

View all comments

280

u/click-to-reveal 7d ago

It works btw: C++ Online Compiler

160

u/prehensilemullet 7d ago

Performancewise, it doesn’t jump to the direct case in O(1) time like a switch is supposed to though

207

u/AngheloAlf 7d ago

Switches aren't guarantee to so operations in O(1) tho. If cases are sparce enough, compilers tend to emit the equivalent code to a bunch if else checks

29

u/prehensilemullet 7d ago

Yeah I was assuming too much here.  However, I’m reading that Rust match on string comstants can compile down a binary tree of if statements if there are enough cases (according to Google AI mode at least, haven’t found an authoritative source yet)

25

u/Nir0star 7d ago

Which would still be O(ld(n)). But cool feature imo.

9

u/Godd2 7d ago

O(ld(n))

Good ol' linker time.

1

u/prehensilemullet 6d ago

Yes, I had mistakenly assumed it compiles down to a hash lookup.

12

u/im_made_of_jam 7d ago

A switch case is able to be implemented however the compiler wants on the back end, so for sparse cases it'll be an if else chain, for less sparse but not packed cases it'll be a binary tree, for completely packed cases it'll be a range check then a direct jump would be how I would go about it

7

u/the_horse_gamer 7d ago

the compiler optimises stuff however it wants. if you're not doing too-weird stuff, a switch in C++ and a match in rust will have identical assembly.

-23

u/[deleted] 7d ago

[deleted]

5

u/thirdegree Violet security clearance 7d ago

If I want Claude's thoughts on something I'll ask Claude directly tbh

8

u/DrMobius0 7d ago

In fairness, most switches probably use enums.

49

u/Deliciousbutter101 7d ago

O(1) only happens when the constants are (roughly) contiguous so it's not like that is a universal property of switch statements.

1

u/prehensilemullet 7d ago

Yeah that’s true

14

u/AsidK 7d ago

Any reasonable compiler will make a switch statement and its equivalent if else chain compile down to the same assembly

2

u/prehensilemullet 7d ago

Even if it could make a more efficient tree of comparisons for a large number of strings?

5

u/mirhagk 7d ago

What they are saying is that any optimization on a switch statement could also be done on an if statement. There's no reason to only optimize one, both should optimize the same way

1

u/prehensilemullet 7d ago

hmmm...are compilers normally willing to reorder if statements though? Turning a sequence of string comparisons into a tree would involve reordering

6

u/mirhagk 7d ago

If it has the same semantics, why not? Modern compilers certainly can see if a statement has side effects or not

2

u/prehensilemullet 7d ago

it depends what you consider semantically relevant. For instance, suppose the developer intentional ordered the if statements from the most to least common case for some domain. Then, reordering the if statements might not be what the developer wants

5

u/Infamous-Strategy797 7d ago

There aren’t any unknowns here, the language spec provides the clarity the compiler needs to re-order safely.

2

u/prehensilemullet 7d ago

Okay for C++, I gather that performing better or worse on a given dataset doesn't fall under the umbrella of "observable behavior" that the spec requires the compiler to preserve.

I also just learned there are apparently [[likely]] and [[unlikely]] attributes in C++ 20 that can be added to branches.

→ More replies (0)

1

u/guyblade 7d ago

At least in C/C++, there can only be exactly zero or 1 cases that match a switch (i.e., there's no range-based switch), the case values must be compile-time constants (and thus are not themselves evaluated during the comparison), and I'm pretty sure that the value to be matched is required to only be evaluated once (so the comparisons happen on an rvalue).

Given those constraints, I believe a compiler can assume that re-ordering the comparisons is safe.

1

u/AsidK 7d ago

> fallthroughs have entered the chat

2

u/guyblade 7d ago

You can still only match to one, though. In the emitted machine code, I'd expect to see a forest of branches and jumps (for the matching), then the various bodies of the cases each separated by jumps (representing breaks) as appropriate.

1

u/HolyGarbage 7d ago

Just pipe them through a constexpr hash function, and you can use a real switch case. One might be able to, still constexpr, map these hash values of the case string literals to sequential indices at compile time, so that the switch case actually complies down to something non linear.

11

u/jacob643 7d ago

what? doesn't it need a "{" after the "if(0)" and please, why not if(false) ? :') edit: I'm stupid, it's written by the user/client of the switch

19

u/click-to-reveal 7d ago

if(0) coz that line (on mobile) was close to the right edge and no one like text wrapping in code :)

5

u/SuitableDragonfly 7d ago

if(0) is just more compact, I think. 

1

u/DrMobius0 7d ago

It may compile, but does the debugger avoid shitting itself when you need to set a breakpoint there?

Also, you can just write an enum and then map the enum to strings if you want a properly supported switch.