T[new]

dsimcha dsimcha at yahoo.com
Sun Aug 9 19:35:15 PDT 2009


== Quote from bearophile (bearophileHUGS at lycos.com)'s article
> Walter Bright:
> > Under the hood, a T[new] will be a single pointer to a library defined
> > type. This library defined type will likely contain three properties:
> I have another question: are there some performance penalities in using such
arrays for normal random access operations?
> Bye,
> bearophile

import std.stdio, std.perf;

final class TNew {
    uint* foo;
    uint length;

    this(uint size) {
        foo = (new uint[size]).ptr;
        length = size;
    }

    ref uint opIndex(uint index) {
        return foo[index];
    }
}

void main() {
    auto pc = new PerformanceCounter;

    uint[] array = new uint[1];
    pc.start;
    foreach(i; 0..100_000_000) {
        array.ptr[0]++;
    }
    pc.stop;
    writeln("Direct:  ", pc.milliseconds);

    auto tnew = new TNew(1);
    pc.start;
    foreach(i; 0..100_000_000) {
        tnew[0]++;
    }
    pc.stop;
    writeln("Indirect:  ", pc.milliseconds);
}

Results:

Direct:  226
Indirect:  229



More information about the Digitalmars-d mailing list