4 ms·
Just do (x>y)-(x<y). It's actually pretty fast: http://stackoverflow.com/questions/10996418/efficient-integer-compare-function/10997428#10997428 http://stackove
by ambrop7 12y ago
Just do (x>y)-(x<y). It's actually pretty fast: http://stackoverflow.com/questions/10996418/efficient-integer-compare-function/10997428#10997428 http://stackoverflow.com/questions/10996418/efficient-intege...
Disclaimer: not actually my original invention.
- pkhuong 12y agoThat's a good one for integers and if you don't intend the comparator to be inlined. For inlined comparison, I prefer if (x == y) return 0; return (x < y) ? -1 : 1;. The control flow is more transparent and C compilers are usually able to perform the if/if conversion necessary to get the single comparison you'd expect in hand-rolled sorts or searches.