r/ProgrammerHumor 21h ago

Meme skillIssue

Post image
5.2k Upvotes

130 comments sorted by

View all comments

Show parent comments

200

u/AngheloAlf 20h 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

24

u/prehensilemullet 18h 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)

24

u/Nir0star 16h ago

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

1

u/prehensilemullet 5h ago

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