Allocating many object

Derek Parnell derek at psych.ward
Mon Mar 27 15:32:00 PST 2006


On Mon, 27 Mar 2006 21:05:55 +0200, Frank Benoit wrote:

> class Test
> {
>     int[10] values;
> }
> 
> void main()
> {
>     Test[1000_000] arr;
>     printf( "sizeof Test : %d\n\x00", Test.classinfo.init.length );
>     printf( "sizeof Array: %d\n\x00", arr.sizeof );
>     for( int j = 0; j < 1000; j++ )
>     {
>         for( int i = 0; i < 1000; i++ )
>         {
>             arr[j*1000+i] = new Test;
>         }
>         printf( "loop %d\n\x00", j );
>     }
> }
> 
> The program runs and gets slower and slower.
> After 8min at loop 700 I canceled.
> Is this speed acceptable for allocating 48MB (+4MB for the array)?
> 
> I tried to use a XML DOM parser, parsing 1,5MB xml. This seems to take
> endless.
> 
> Is there a workaround? e.g. preallocating memory in one piece.

I made a slight change to the code and it worked well.

I moved 'Test[1000_000] arr;' to the module level so it wouldn't be stack
allocated, and I changed it to 'Test[1000_000] arr = void;' to avoid
preinitializing it.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
28/03/2006 10:30:30 AM



More information about the Digitalmars-d mailing list