3 ms·
<=> and cmp also have the benefit that fewer comparisons are done. It might not be that big a deal for Perl scalars, but for C++, an overloaded operator< that
by omoikane 5y ago
<=> and cmp also have the benefit that fewer comparisons are done. It might not be that big a deal for Perl scalars, but for C++, an overloaded operator< that does something like this:
return left.a != right.a ? left.a < right.a
: left.b < right.b;
That might result in exponential number of compare operations if those nested comparisons are also overloaded, whereas with <=> (as of C++20), the number of compare operations can be made to be linear:
return left.a <=> right.a < 0 || left.b <=> right.b < 0;
- omoikane 5y agoSorry, that should have been: return !(left.a <=> right.a >= 0 && left.b <=> right.b >= 0);