EnsureCapacity ensures that capacity is *at least* the request size.
If you examine the code for EnsureCapacity, you will see that each
time the capacity has to be adjusted, it is doubled. You can also try
the following program to dump the capacity:
using System;
using System.Collections;
class MyApp {
public static void Main() {
ArrayList l = new ArrayList();
for(int i=0;i<33; i++) {
l.Add(i);
Console.WriteLine("Iteration {0}. Capacity {1}", i, l.Capacity);
}
}
}
Regards,
Pradeep
http://www.tapadiya.net/pradeep
----- Original Message -----
From: "kojiishi" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Sunday, July 07, 2002 5:38 PM
Subject: Re: [ADVANCED-DOTNET] Specifying an initial capacity hurts
performance [was Benchmark]
> > these arrays grow themselves exponentially
> Are you sure about this? Where did you get this information?
>
> As far as I can tell by looking at mscorlib.dll using dis-compiler,
> ArrayList.Add() uses:
> if (this._size == (int) this._items.Length)
> this.EnsureCapacity(this._size + 1);
>
> -----Original Message-----
> From: Moderated discussion of advanced .NET topics.
> [mailto:[EMAIL PROTECTED]] On Behalf Of Ian Griffiths
> Sent: Sunday, July 07, 2002 3:22 AM
> To: [EMAIL PROTECTED]
> Subject: Re: [ADVANCED-DOTNET] Specifying an initial capacity hurts
> performance [was Benchmark]
>
>
> "David Ferguson" <[EMAIL PROTECTED]> wrote:
> >
> > Certainly in this test (large array, constant value) it
> > does appear to be slower (not significantly, but a little).
> >
> > >By value, dynamically reallocated | >By value, preallocated
> > | (with pause between test)
> > >----Test 0 | >----Test 0
> > >Elapsed 640 ms | >Elapsed 661 ms
> > >----Test 1 | >----Test 1
> > >Elapsed 590 ms | >Elapsed 651 ms
> > >----Test 2 | >----Test 2
> > >Elapsed 691 ms | >Elapsed 651 ms
> > >----Test 3 | >----Test 3
> > >Elapsed 621 ms | >Elapsed 651 ms
> > >----Test 4 | >----Test 4
> > >Elapsed 601 ms | >Elapsed 651 ms
> > ---------------------- ------------------------------
> > average 629 average 653
>
> But the variance is pretty huge on your first column there. I think the
> different of 24ms is swamped by the uncertainty in the measurements.
>
> So with your data I came to the same conclusion as I did with my data:
> it's difficult to tell which, if either, is faster, without more
> extensive measurement and careful analysis.
>
> This is what you would expect - the difference in allocation count for
> preallocation will only amount to a fairly small number (these arrays
> grow themselves exponentially, so the number of allocations is small).
> So you'd expect it to be too small to measure easily.
>
>
> --
> Ian Griffiths
> DevelopMentor
>
> You can read messages from the Advanced DOTNET archive, unsubscribe from
> Advanced DOTNET, or subscribe to other DevelopMentor lists at
> http://discuss.develop.com.
>
> You can read messages from the Advanced DOTNET archive, unsubscribe from
Advanced DOTNET, or
> subscribe to other DevelopMentor lists at http://discuss.develop.com.
>
>
You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.