/ / El tipo de colección más rápido para agregar y enumerar - c #

Tipo de recopilación más rápido para agregar y enumerar - c #

¿Cuál es el tipo de colección más rápido cuando no lo hago?me importa los duplicados, el orden, etc. cuando solo quiero agregarle un tipo específico de objeto y luego recorrer la colección con un bucle for. No sé cuántos artículos se agregarán.

Actualmente estoy usando una lista (de) como una matrizme obliga a saber el tamaño que no conozco. Pero es un cuello de botella cuando la colección crece a un tamaño de millones de objetos. ¿Qué puede ser más rápido / mejor que una lista (de) en?

He leído algunas preguntas similares antes. Pero podría estar equivocado, pero sentí que un hashset sería el propósito equivocado, ya que la idea de un conjunto no es lo que voy a hacer con él. Una matriz como la que dije me obliga a saber el tamaño antes de agregar elementos. Un diccionario es un propósito equivocado.

Respuestas

1 para la respuesta № 1

No creo que List sea un cuello de botella, aquí está el programa de prueba:

for (int max = 10000; max <= 10000000; max *= 10)
{
List<string> list = new List<string>();
LinkedList<string> linkedlist = new LinkedList<string>();
Queue<string> queue = new Queue<string>();
HashSet<string> hashset = new HashSet<string>();
string[] array = new string[max];

Random rand = new Random();
string value;

DateTime start = DateTime.Now;
for (int i = 0; i < max; ++i)
list.Add(rand.Next().ToString());
for (int i = 0; i < max; ++i)
value = list[i];
DateTime dtlist = DateTime.Now;

for (int i = 0; i < max; ++i)
linkedlist.AddLast(rand.Next().ToString());
var head=linkedlist.First;
for (int i = 0; i < max; ++i)
{
value = head.Value;
head = head.Next;
}
DateTime dtlinkedlist = DateTime.Now;

for (int i = 0; i < max; ++i)
queue.Enqueue(rand.Next().ToString());
for (int i = 0; i < max; ++i)
value = queue.Dequeue();
DateTime dtqueue = DateTime.Now;

for (int i = 0; i < max; ++i)
hashset.Add(rand.Next().ToString());
var ihash=hashset.GetEnumerator();
for (int i = 0; i < max; ++i)
{
value = ihash.Current;
ihash.MoveNext();
}
DateTime dthashset = DateTime.Now;

for (int i = 0; i < max; ++i)
array[i] = rand.Next().ToString();
for (int i = 0; i < max; ++i)
value = array[i];
DateTime dtarray = DateTime.Now;


Console.WriteLine("List " + list.Count + ": " + new TimeSpan(dtlist.Ticks - start.Ticks).TotalSeconds);
Console.WriteLine("LinkedList " + linkedlist.Count + ": " + new TimeSpan(dtlinkedlist.Ticks - dtlist.Ticks).TotalSeconds);
Console.WriteLine("Queue " + queue.Count + ": " + new TimeSpan(dtqueue.Ticks - dtlinkedlist.Ticks).TotalSeconds);
Console.WriteLine("HashSet " + hashset.Count + ": " + new TimeSpan(dthashset.Ticks - dtqueue.Ticks).TotalSeconds);
Console.WriteLine("Array " + array.Length + ": " + new TimeSpan(dtarray.Ticks - dthashset.Ticks).TotalSeconds);
Console.WriteLine();
}

Y esta es la salida en mi PC:

List 10000: 0,0070058
LinkedList 10000: 0,0010009
Queue 0: 0,0020004
HashSet 10000: 0,0019973
Array 10000: 0,0040013

List 100000: 0,0139995
LinkedList 100000: 0,0270084
Queue 0: 0,0239972
HashSet 99992: 0,0320128
Array 100000: 0,0229999

List 1000000: 0,225034
LinkedList 1000000: 0,2970565
Queue 0: 0,2606011
HashSet 999767: 0,4960486
Array 1000000: 0,2189983

List 10000000: 2,3172126
LinkedList 10000000: 3,4592683
Queue 0: 3,1272267
HashSet 9976601: 6,2188591
Array 10000000: 2,3435249