Array append performance

bearophile bearophileHUGS at lycos.com
Fri Aug 22 16:30:32 PDT 2008


I have performed another small benchmark of the dynamic array append, similar to one I have performed in the past, a comparison between the C++ push_back of C++ and the ~= of D, both performed after a "reserve".

To perform the "reserve" in D I have used std.gc.malloc instead of the simpler:
auto a = new int[n];
a.length = 0;
to actually speed up the D code a little, avoiding a (fast but useless) memory cleaning.

I have used MinGW 4.2.1 and DMD v.1.034.

Compiling the C++ with:
-O3 -s
and the D with:
-O -release -inline

The following results are with n = 100_000_000 (about 400 MB RAM used):
  D:   7.94 s
  C++: 0.60 s  (13.2X)

Note that if n is 10 or even 100 times smaller the situation doesn't change much:
n = 10_000_000:
  D:   0.83 s
  C++: 0.09 s

Note2: In the D code the final deallocation of the array is fast enough, and its allocation is fast too (you can see this timing just the for loop in the middle of the program). The really slow part seems to be the loop itself.

Note3: If a reserve isn't used (that is in both programs the arrays aren't pre-allocated) the difference in performance becomes smaller, about 8.6X with n = 100_000_000).

This is the asm generated by g++:
http://codepad.org/hXNDH0se

Bye,
bearophile

---------------------------
C++ code:

#include "stdio.h"
#include <vector>

using namespace std;

int main(int argc, char **argv) {
    int n = argc >= 2 ? atoi(argv[1]) : 1000;

    vector<int> a;
    a.reserve(n);
    for (int i = 0; i < n; ++i)
        a.push_back(i);

    printf("%d\n", a[n-1]);

    return 0;
}

---------------------------
D code:

import std.conv: toInt;
import std.gc: malloc, hasNoPointers;

int main(string[] args) {
    int n = args.length >= 2 ? toInt(args[1]) : 1000;

    int* aux_ptr = cast(int*)malloc(n * int.sizeof);
    hasNoPointers(aux_ptr);
    int[] a = aux_ptr[0 .. 0];

    for (int i; i < n; i++)
        a ~= i;

    printf("%d\n", a[n - 1]);
    return 0;
}



More information about the Digitalmars-d mailing list