Array append performance

Fawzi Mohamed fmohamed at mac.com
Wed Aug 27 15:37:01 PDT 2008


On 2008-08-28 00:22:21 +0200, Fawzi Mohamed <fmohamed at mac.com> said:

> On 2008-08-27 17:20:55 +0200, bearophile <bearophileHUGS at lycos.com> said:
> 
>> Sean Kelly:
>>> Why not just change that to this instead:
>>> 
>>> switch(i) {
>>> case 8: a[7] = b[7];
>>> case 7: a[6] = b[6];
>>> case 6: a[5] = b[5];
>>> case 5: a[4] = b[4];
>>> case 4: a[3] = b[3];
>>> case 3: a[2] = b[2];
>>> case 1: a[0] = b[0];
>>> case 0: break;
>>> default: memcpy( a.ptr, b.ptr, a.length * DataType.sizeof );
>>> }
>>> 
>>> The Range stuff is cool and all, but not really necessary :-)
>> 
>> I have benchmarked many alternative versions, your one too, but mine is 
>> significantly faster than your one on the DMD I am using. Don't ask me 
>> why.
> 
> memory ordering probably, looping from 0 to 7 is faster than 7 to 0...

so
int j=j;
switch(i) {
case 8: a[j] = b[j]; ++j;
case 7: a[j] = b[j]; ++j;
case 6: a[j] = b[j]; ++j;
case 5: a[j] = b[j]; ++j;
case 4: a[j] = b[j]; ++j;
case 3: a[j] = b[j]; ++j;
case 1: a[j] = b[j];
case 0: break;
default: memcpy( a.ptr, b.ptr, a.length * DataType.sizeof );
}

should be better...
Fawzi

> 
>> 
>> You can find the Range!([start,] stop[, step]) in my libs, of course ;-)
>> 
>> Bye,
>> bearophile





More information about the Digitalmars-d mailing list