using a Arraylist.add() if you have a large file..you mite as well
take a vacation...so this is not a option
Sorry, but that just simply isn't so (metrics below using
List<string>, but since string isn't boxed they should be quite
comparable). List<Tuses doubling, so it resizes itslef very quickly.
By my metrics, the List<Tapproach is slower, yes, but only by a
small factor (~5). Given the amount of IO involved, this is nothing -
i.e. stroring 1M strings in a List<Ttakes 59ms on my lowly laptop.
Maybe you take short vacations... I think all the faff you are doing
(including rewinding the stream) will bring them quite close.
In the test, for *both* cases I am simply storing nulls. Since we are
talking reference types, this is perfectly fine and doesn't affect the
results. The memory *in the list and array* is the same either way.
Again, most of the weight it either approach would be due to memory /
IO requirements for the *actual* data, which does not depend on the
storage mechanism.
output:
{size} {ticks for list} vs {ticks for fixed array} {multiplier}
({ms for list})
5 37 vs 6: 6.16666666666667 (0)
10000 3118 vs 602: 5.17940199335548 (0)
100000 23339 vs 4039: 5.77841049764793 (6)
1000000 214532 vs 50344: 4.261322103925 (59)
10000000 2606905 vs 472739: 5.51446992949598 (728)
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
static class Program {
static void Main() {
Test(5); // to get JIT etc
Test(10000);
Test(100000);
Test(1000000);
Test(10000000);
}
static void Test(int size) {
Stopwatch watch = new Stopwatch();
watch.Start();
List<stringlist = new List<string>();
for (int i = 0; i < size; i++) {
list.Add(null);
}
watch.Stop();
long tickList = watch.ElapsedTicks;
long msList = watch.ElapsedMilliseconds;
watch = new Stopwatch();
watch.Start();
string[] array = new string[size];
for(int i = 0; i < size; i++) {
array[i] = null;
}
watch.Stop();
long tickArray = watch.ElapsedTicks;
Console.WriteLine("{0}\t{1} vs {2}:\t{3} ({4})", size,
tickList, tickArray,
(tickList * 1.0) / tickArray, msList);
}
}