Array append performance
Walter Bright
newshound1 at digitalmars.com
Tue Aug 26 23:11:52 PDT 2008
The bugzilla report on this suggests replacing []=[] with memcpy. But
this benchmark shows this isn't a consistent win:
import std.c.string;
long timer()
{
asm
{ naked ;
rdtsc ;
ret ;
}
}
void test1(byte[] a, byte[] b)
{
a[] = b[];
}
void test2(byte[] a, byte[] b)
{
memcpy(a.ptr, b.ptr, a.length);
}
void main()
{
for (int i = 4; i < 100_000_000; i *= 2)
{
auto a = new byte[i];
auto b = new byte[i];
auto start = timer();
test1(a, b);
auto end = timer();
auto r1 = end - start;
start = timer();
test2(a, b);
end = timer();
auto r2 = end - start;
printf("i: %8d,\t[]=[]: %8lld,\tmemcpy: %8lld\n", i, r1, r2);
}
}
Running this program produces:
i: 4, []=[]: 144, memcpy: 568
i: 8, []=[]: 144, memcpy: 300
i: 16, []=[]: 172, memcpy: 324
i: 32, []=[]: 204, memcpy: 344
i: 64, []=[]: 288, memcpy: 276
i: 128, []=[]: 288, memcpy: 272
i: 256, []=[]: 352, memcpy: 364
i: 512, []=[]: 372, memcpy: 424
i: 1024, []=[]: 552, memcpy: 564
i: 2048, []=[]: 684, memcpy: 1384
i: 4096, []=[]: 1344, memcpy: 1772
i: 8192, []=[]: 2900, memcpy: 3216
i: 16384, []=[]: 5292, memcpy: 5472
i: 32768, []=[]: 11496, memcpy: 10388
i: 65536, []=[]: 29484, memcpy: 27480
i: 131072, []=[]: 110464, memcpy: 67784
i: 262144, []=[]: 655580, memcpy: 562400
i: 524288, []=[]: 1204124, memcpy: 1107256
i: 1048576, []=[]: 2364588, memcpy: 2272552
i: 2097152, []=[]: 4516440, memcpy: 4417764
i: 4194304, []=[]: 8996992, memcpy: 8817176
i: 8388608, []=[]: 20223908, memcpy: 17717748
i: 16777216, []=[]: 35774952, memcpy: 36094652
i: 33554432, []=[]: 71008068, memcpy: 71246896
i: 67108864, []=[]: 142982284, memcpy: 145473300
There's not much of a consistent conclusion to be drawn from that.
More information about the Digitalmars-d
mailing list