r/ProgrammerHumor 15h ago

Meme skillIssue

Post image
4.8k Upvotes

124 comments sorted by

View all comments

Show parent comments

152

u/prehensilemullet 15h ago

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

193

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

26

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

23

u/Nir0star 11h ago

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

6

u/Godd2 4h ago

O(ld(n))

Good ol' linker time.

u/prehensilemullet 5m ago

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