Array append performance 2

bearophile bearophileHUGS at lycos.com
Mon Sep 1 04:41:53 PDT 2008


Denis Koroskin:
> Please, test my version, too!
> While it is slighly slower, it allows appending invariant arrays (i.e.  
> strings) without copying data (although it is currently not implemented).

Sorry, I'm not using D 2.x. I (hopefully) will soon show an improved version of my code, so you can test your version.

Note: I have benchmarked the version with the deque-like data structure, and it's slower, the code is a bit more complex (and I may have hit a bug in the GC (I'd like to know if Tango behaves at the same way)), so I'll probably just use the first version, with the reallocs.

----------------

With Phobos:

import std.stdio: writefln;
import std.gc: malloc;

void main() {
    int M = 1 << 16;
    int N = M / int.sizeof;
    const int N_POINTERS = 18;

    int*[N_POINTERS] pointers;
    foreach (ref ptr; pointers) {
        ptr = cast(int*)malloc(N * int.sizeof);
        if (ptr is null) {
            throw new Error("OUT OF MEM");
            return 1;
        }
    }

    int last = 0;
    foreach (i, ptr; pointers) {
        int int_ptr = cast(int)ptr;
        int diff = int_ptr - last;
        writefln(i, " ", int_ptr, " ", diff, " ", M - diff);
        last = int_ptr;
    }
}

Prints:

0 9052160 9052160 -8986624
1 9117696 65536 0
2 9183232 65536 0
3 9248768 65536 0
4 9314304 65536 0
5 9379840 65536 0
6 9445376 65536 0
7 9510912 65536 0
8 9576448 65536 0
9 9641984 65536 0
10 9707520 65536 0
11 9773056 65536 0
12 9838592 65536 0
13 9904128 65536 0
14 9969664 65536 0
15 10092544 122880 -57344
16 10158080 65536 0
17 10223616 65536 0

Is that result at n = 15 normal/correct?

Bye,
bearophile



More information about the Digitalmars-d mailing list