AAs of struct or array

bearophile bearophileHUGS at lycos.com
Thu May 27 05:33:23 PDT 2010


I have noticed a significant speed difference between foo1 and foo2 (D2 code):

import std.c.stdio: printf;

int foo1(int x, int y) {
    static int[int[2]] cache;
    int[2] args = [x, y];
    cache[args] = x;
    return x;
}

int foo2(int x, int y) {
    static struct Pair { int x, y; }
    static int[Pair] cache;
    Pair args = Pair(x, y);
    cache[args] = x;
    return x;
}

void main() {
    enum int N = 600;
    int tot;
    foreach (x; 1 .. N)
        foreach (y; 1 .. N)
            tot += foo2(x, y); // use foo1 or foo2 here
    printf("%d\n", tot);
}


Bye,
bearophile


More information about the Digitalmars-d mailing list