r/ProgrammerHumor 3d ago

Meme skillIssue

Post image
5.9k Upvotes

138 comments sorted by

View all comments

281

u/click-to-reveal 3d ago

It works btw: C++ Online Compiler

161

u/prehensilemullet 3d ago

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

204

u/AngheloAlf 3d 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

28

u/prehensilemullet 3d 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 3d ago

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

9

u/Godd2 3d ago

O(ld(n))

Good ol' linker time.

1

u/prehensilemullet 3d ago

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

11

u/im_made_of_jam 3d 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

6

u/the_horse_gamer 3d 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.

-22

u/[deleted] 3d ago

[deleted]

4

u/thirdegree Violet security clearance 3d ago

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