www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Small performance problem

In D there are several things to implement and fix that have a priority higher
than tuning the performance of the DMD back-end (a work may be wasted time
anyway). But arrays are common, so the following may interest anyway.

On LDC this synthetic benchmark shows the same run time with both values of the
use_dynamic_array constant, while on DMD the version that uses dynamic arrays
is something like 70% faster (on both D1 compiler and last D2 compiler):

version (Tango)
    import tango.stdc.stdio: printf;
else
    version (D_Version2)
        import std.c.stdio: printf;

const bool use_dynamic_array = true; // change this

void main() {
    const int n = 100_000;
    const int nloop = 6_000;

    int[] aux1 = new int[n];
    int[] aux2 = new int[n];

    static if (use_dynamic_array) {
        alias aux1 a1;
        alias aux2 a2;
    } else {
        int* a1 = aux1.ptr;
        int* a2 = aux2.ptr;
    }

    for (int i; i < nloop; i++) {
        for (int j; j < n; j++)
            a1[j] += a2[j];
        for (int j; j < n; j++)
            a2[j] += a1[j];
    }

    printf("%d\n", a1[10]);
}

Asm of the inner loop with dynamic arrays (DMD):
L59:	mov	ECX,[EBX*4][ESI]
	add	[EBX*4][EDX],ECX
	inc	EBX
	cmp	EBX,0186A0h
	jb	L59

Asm of the inner loop with pointers (DMD):
L47:	mov	ESI,01Ch[ESP]
	mov	EAX,014h[ESP]
	mov	EDI,[EBX*4][ESI]
	add	[EBX*4][EAX],EDI
	inc	EBX
	cmp	EBX,0186A0h
	jb	L47

Bye,
bearophile
Oct 22 2009