4 ms·
> This is exceedingly interesting to me because you'd expect Compare to cost more than < (it does more stuff), and yet people are somehow baited into writing it
by Dylan16807 4y ago
> This is exceedingly interesting to me because you'd expect Compare to cost more than < (it does more stuff), and yet people are somehow baited into writing it.
I would expect it to be the same within a couple nanoseconds, because "more stuff" is an extremely small amount of stuff.
The naive direct implementation of a 3 way compare does the exact same comparisons as a direct implementation of less than, it merely returns more information, which makes it more generally useful.
- jrockway 4y agoYeah. To me, I just think it's weird to ask for two pieces of information and then throw one of them away. Sometimes it's not a performance hit, but I feel like it's one of those things where you should get a feeling "I might be doing this wrong". But if you don't know that < exists (and obviously searched for strings.Less), then you won't get that feeling. That's what I find so fascinating; if other people's feelings are similar to mine, then they simply didn't know that you were allowed to use < here.
- Dylan16807 4y agoI don't see it as two pieces of information. It's returning one idea either way, but the tri-state tells you more than a boolean.
- coding123 4y agoBut that is more useful, we should agree, for implementing binary trees or sorting algorithms.