Yet another strike against the current AA implementation

bearophile bearophileHUGS at lycos.com
Mon Apr 27 04:38:30 PDT 2009


dsimcha:
> Rule number 1 of all performance discussions:  Always measure if possible.

Very good, thank you. I have run your code, with a bit of changes:

import std.stdio: writeln;
import std.perf: PerformanceCounter;

enum uint N = 1_000_000_000;

struct Direct {
    uint n;
    uint front() { return n; }
    void popFront() { n++; }
    bool empty() { return n >= N; }
}


struct Apply {
    int opApply(int delegate(ref uint) dg) {
        int res;
        foreach (i; 0U .. N) {
            res = dg(i);
            if(res)
                break;
        }
        return res;
    }
}


class Virtual {
    uint n;
    uint front() { return n; }
    void popFront() { n++; }
    bool empty() { return n >= N; }
}


void main() {
    scope pc = new PerformanceCounter;
    pc.start;
    foreach (elem; Direct.init) {}
    pc.stop;
    writeln("Direct:         ", pc.milliseconds);

    pc.start;
    foreach (elem; Apply.init) {}
    pc.stop;
    writeln("opApply:        ", pc.milliseconds);

    pc.start;
    auto v = new Virtual;
    foreach (elem; v) {}
    pc.stop;
    writeln("Virtual:        ", pc.milliseconds);

    pc.start;
    scope auto v2 = new Virtual;
    foreach (elem; v2) {}
    pc.stop;
    writeln("Scoped virtual: ", pc.milliseconds);
}

As you see I have added a version with scoped class at the bottom hoping to improve performance a bit, but the result is the opposite, can you explain me why?

Direct:         2699
opApply:        3520
Virtual:        7543
Scoped virtual: 8550

Run on a Core2 2 GHz, on WinXP.

Bye,
bearophile



More information about the Digitalmars-d mailing list