memory management of array

Bill Baxter wbaxter at gmail.com
Fri Dec 19 14:01:48 PST 2008


On Sat, Dec 20, 2008 at 12:55 AM, ZHOU Zhenyu <rinick at goozo.net> wrote:
> I use D to solve the programming problems  on projecteuler ( http://projecteuler.net/ )
>
>
> I thought D should be as fast as C++, but it turns out that sometimes D is much slower.
>
> It seems that array would reallocate its memory every time the  ~= operation is invoked.
> If I push 10^7 numbers into an array, the realloc would be called for 10^7 times.
> That would be at least 100 times slower than C++STL.
>
>
> So I don't use ~= and manage the length of the array myself
> When the length is not enough, I reallocate the memory with the size of  requiredSize * 9 / 8. ( In STL, it's oldSize *  2. )
> 9/8 could save a lot of memory, and the attached source code shows that *9/8 is almost as fast as *2 .
>
> My question is:
> Is there any other way to solve this problem?
> I think this kind of optimization should be implemented in the compiler or the D runtime.

In addition to what other people have said, if you know the length the
array will be eventually you can preallocate by doing this:

float[] arr;
arr.length = N;
arr.length = 0;

And you can put that into a little template function called "reserve" like so:

void reserve(ref T[] x, size_t N) {
    auto len = x.length;
    if (N > len) {
        x.length = N;
        x.length = len;
    }
}

and use it like this:

float[] arr;
arr.reserve(N);

--bb



More information about the Digitalmars-d mailing list