Allocating many object

Chris Nicholson-Sauls ibisbasenji at gmail.com
Mon Mar 27 11:58:19 PST 2006


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 used the following re-write of your program:

# private import std.stdio;
# private import cashew.utils.timer;
#
# class Test {
#   int[10] values ;
# }
#
# void main () {
#   Test[100_000] arr;
#   writefln(r"sizeof Test : %d"c, Test.classinfo.init.length);
#   writefln(r"sizeof Array: %d"c, arr.sizeof);
#   { auto Timer t0 = new Timer(r"loop"c);
#   foreach(i, inout x; arr) {
#     x = new Test;
#     if (((i + 1) % 100) == 0)
#       writefln(r"loop %d"c, i + 1);
#   }
#   }//timer
# }

Do note I dropped the size of 'arr' from 1000000 to 100000.  This has nothing to do with 
with the issue, so far as I know, and everything to do with a stack size problem on the 
machine I'm sitting at right now.  Don't ask... pos.

Anyhow, the Timer reports from about thirty or so runs of that program spanned between 
0.66 seconds and 0.77 seconds total, so I shouldn't expect your original to take more than 
a second or two.

-- Chris Nicholson-Sauls



More information about the Digitalmars-d mailing list