Hi,
>>Don't know why the speed difference but have you tried pre-setting the capacity of list using the Capacity property (in both scenarios)?
>>Best,
>>Viv
>
>Just did, Viv
>
>There is no difference, and I would not have expected one since there are 6000000 iterations and the list must be expanded to its capacity after the first iteration
True. So maybe a more representative test would be to create the List within the loop.I tried this:
int nTimes = 600000;
int Size = 10000;
private void UseWithCapacity()
{
int[] buf = new int[Size];
for (int i = nTimes; --i != 0; )
{
List<int> list = new List<int>();
list.Capacity = Size; //With: 1:32 Without: 1:52
foreach (int item in buf)
list.Add(item);
}
}
and, as annotated, there was some improvement, tho not huge, when the capacity was pre-defined. When using AddRange() setting the Capacity in this way gave no discernable benifit....
>
>
>I was quite surprised to find that out
>
>[Context]
>I'm playing with streams, subclassed the stream to implement a base64 encoder/decoder
>
>The problem with ToBase64Transform and FromBase64Transform is that you have to buffer the input, transform (pieces of )the input [ you do not know in advance whether to call TransformBlock or TransformFinalBlock ], and put the transformations on an outputbuffer
>
>
>protected Queue<Byte> InBuffer = new Queue<byte>();
>protected Queue<Byte> OutBuffer = new Queue<byte>();
>
>
>The Read and Write of the stream work with arrays
>
>So I tried - just for fun - how it would perform if I were to use List(byte)
>
>I started testing with different array sizes - and found out that the List() is actually quite fast compared to the Queue
>
>[ and the list is slower with many items compared to the queue ]
Interesting. Roughly at what point would Queue become preferable ?
Regards,
Viv