memory management of array

ZHOU Zhenyu rinick at goozo.net
Fri Dec 19 07:55:15 PST 2008


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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ArrayMemory.d
Type: application/octet-stream
Size: 1504 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20081219/f49bfe4f/attachment.obj>


More information about the Digitalmars-d mailing list