www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - A little class benchmark

I have found a little benchmark on the StackOverflow site:

I have written a D2 version that compares few alternatives:

import std.c.stdio: printf;

final class Tester {
    this(int[] arr_) {
        arr = arr_;

    int doAdd1() {
        tot = 0;
        for (int i = 0; i < arr.length; i++)
            tot += arr[i];
        return tot;

    int doAdd2() {
        tot = 0;
        foreach (x; arr)
            tot += x;
        return tot;

    int doAdd3() {
        int sum = 0;
        foreach (x; arr)
            sum += x;
        tot = sum;
        return tot;

    int doAdd4() {
        int sum = 0;
        immutable int len = arr.length;
        for (int i = 0; i < len; i++)
            sum += arr[i];
        tot = sum;
        return tot;

        int[] arr;
        int tot;

void main() {}

The asm produced by the latest dmd2, optimized build:

Inner loop of Tester.doAdd1:

L10:    mov EDX,0Ch[EBX]
        mov ECX,[ESI*4][EDX]
        inc ESI
        add 010h[EBX],ECX
        mov EAX,8[EBX]
        cmp 8[EBX],ESI
        ja  L10

Inner loop of Tester.doAdd2:

L30:    mov EAX,[EDX*4][EBX]
        inc EDX
        add 010h[ESI],EAX
        cmp EDX,EDI
        jb  L30

Inner loop of Tester.doAdd3:

L2E:    add ECX,[EDX*4][EBX]
        inc EDX
        cmp EDX,010h[ESP]
        jb  L2E

Inner loop of Tester.doAdd4:

L11:    mov EDX,0Ch[EDI]
        mov EAX,8[EDI]
        add EBX,[ESI*4][EDX]
        inc ESI
        cmp ESI,EBP
        jl  L11

Do you know why the compiler uses the instance attribute inside the loop,
instead of a copy of it?

Oct 14 2010