Lexicographic comparison of arrays (of chars)

Jacob Carlborg doob at me.com
Wed Jan 22 15:11:09 UTC 2020


On Wednesday, 22 January 2020 at 14:50:01 UTC, Per Nordlöw wrote:

> Will that incur an extra runtime cost compared to __cmp?

I haven't looked at how `__cmp` is implemented but I would guess 
there's some extra overhead. Need to get type info and then there 
will be several virtual method calls involved. Seems to be one of 
the initial call to `compare` and then one for each element of 
the array.

BTW, why don't you implement `opCmp` with the built-in comparison 
operators. Those are going to get lower to a call to `__cmp`. 
Something like this:

int opCmp()(const scope typeof(this) that) const @nogc
{
     auto a = this[];
     auto b = that[];
     return a < b ? -1 : (a > b);
}

--
/Jacob Carlborg




More information about the Digitalmars-d-learn mailing list