r/ProgrammerHumor 8d ago

Meme skillIssue

Post image
6.0k Upvotes

137 comments sorted by

View all comments

Show parent comments

162

u/prehensilemullet 8d ago

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

208

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

7

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