X[]==Y[] is 7X slower than it should be -- why?
Robert Fraser
fraserofthenight at gmail.com
Fri Jun 27 07:02:10 PDT 2008
Jarrett Billingsley wrote:
> "Bruce Adams" <tortoise_74 at yeah.who.co.uk> wrote in message
> news:op.udecoog1xikks4 at starquake.cybernetics...
> On Fri, 20 Jun 2008 18:40:25 +0100, Jarrett Billingsley
> <kb3ctd2 at yahoo.com> wrote:
>
>
>> extern (C) int _adEq(Array a1, Array a2, TypeInfo ti)
>> {
>> if (a1.length != a2.length)
>> return 0;
>>
>> auto sz = ti.tsize();
>> auto p1 = a1.ptr;
>> auto p2 = a2.ptr;
>>
>> if (sz == 1)
>> // We should really have a ti.isPOD() check for this
>> return (memcmp(p1, p2, a1.length) == 0);
>>
>> for (size_t i = 0; i < a1.length; i++)
>> {
>> if (!ti.equals(p1 + i * sz, p2 + i * sz))
>> return 0;
>> }
>>
>> return 1;
>> }
>>
>
> there is no guarantee that memcpy will return the same result as opEquals.
>
> ...
>
> Why has no-one screamed about this before?
> -----------------
>
> Because that's not how the above function works? It only calls memcmp if
> the compared array has elements of size 1 -- that is, byte[], ubyte[], and
> char[]. For all other types, it calls ti.equals, which will call the opCmp
> method of classes/structs, if any.
How about a struct of size 1?:
More information about the Digitalmars-d
mailing list