Array append performance
Lionello Lunesu
lio at lunesu.remove.com
Wed Aug 27 04:58:18 PDT 2008
The problem is that the timing for the small arrays cannot be trusted as
there's more overhead than actual code being tested. I've changed your
code by doing each test 100_000_000/i times. I've also added 'cpuid' for
synchronization, as Don suggested:
import std.c.string;
long timer()
{
asm
{ naked ;
push ECX;
push EBX;
mov EAX, 0 ;
cpuid ;
pop EBX;
pop ECX;
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 b = new byte[i];
auto a = new byte[i];
test1(a,b);//pre-cache
auto count = 100_000_000 / i;
auto start = timer();
while (count--)
test1(a, b);
auto end = timer();
auto r1 = end - start;
count = 100_000_000 / i;
start = timer();
while (count--)
test2(a, b);
end = timer();
auto r2 = end - start;
printf("i: %8d,\t[]=[]: %8lld,\tmemcpy: %8lld\n", i, r1, r2);
}
}
Results on my Core2 Duo:
i: 4, []=[]: 2752539660, memcpy: 429537312
i: 8, []=[]: 1004458383, memcpy: 229984623
i: 16, []=[]: 492054948, memcpy: 128590560
i: 32, []=[]: 309960657, memcpy: 135892809
i: 64, []=[]: 204971832, memcpy: 79507359
i: 128, []=[]: 131925789, memcpy: 52222626
i: 256, []=[]: 73768896, memcpy: 40184559
i: 512, []=[]: 43827948, memcpy: 28040859
i: 1024, []=[]: 29030985, memcpy: 21078432
i: 2048, []=[]: 21645027, memcpy: 18953199
i: 4096, []=[]: 18099945, memcpy: 16022259
i: 8192, []=[]: 15905007, memcpy: 15399036
i: 16384, []=[]: 15567399, memcpy: 15795747
i: 32768, []=[]: 32244507, memcpy: 30654063
i: 65536, []=[]: 30824595, memcpy: 31286475
i: 131072, []=[]: 32248179, memcpy: 30950757
i: 262144, []=[]: 34880868, memcpy: 31166667
i: 524288, []=[]: 31220802, memcpy: 35813277
i: 1048576, []=[]: 42463341, memcpy: 42121386
i: 2097152, []=[]: 122218776, memcpy: 118839150
i: 4194304, []=[]: 111864294, memcpy: 110864988
i: 8388608, []=[]: 110600226, memcpy: 107638083
i: 16777216, []=[]: 97014645, memcpy: 95789574
i: 33554432, []=[]: 79344468, memcpy: 78277374
i: 67108864, []=[]: 77437629, memcpy: 78478767
Conclusion: memcpy wins big time for i <= 8192.
More information about the Digitalmars-d
mailing list