title: C# List<> Add() method performance
body: <p>I am working with a List&lt;> collection, adding new objects to the collection inside 2 nested loops. There are some 500000 items added to the collection, after the loops finish to execute.</p>
  
  <p>At first, the addition operation runs well, but soon after there can be noticed a decrease in performance, for the last thousands of elements, the delay time is unbearable.</p>
  
  <p>I have tried various tricks (initializing the collection with a certain size - 500000), replacing the List&lt;> with a LinkedList&lt;> collection, but it didn't help too much.</p>
  
  <p>Can you recommend me a tip to solve the problem? I am interesting in changing the structure with a more optimized one - LinkedList&lt;> for instance performs better than List&lt;> with operations such as addition.</p>
  
  <h1>Method which updates the list</h1>
  
  <pre><code>   private void UpdateForecastList(ConcurrentDictionary&lt;Int32, RegistroSalidaProductoPrevision&gt; prediccion, bool soloMejoresMetodos = true)
     {
          foreach (KeyValuePair&lt;int, RegistroSalidaProductoPrevision&gt; kvp in prediccion)
          {
              KeyValuePair&lt;int, RegistroSalidaProductoPrevision&gt; localKvp = kvp;
  
              IList&lt;Prediccion&gt; pExistente = prediccionList.Where(p =&gt; p.Id == localKvp.Key).ToList();
  
              Articulo articulo = (articuloList.Where(a =&gt; a.Id == localKvp.Key)).First();
  
              if (pExistente.Count &gt; 0)
              {
                  foreach (var p in pExistente)
                  {
                      prediccionList.Remove(p);
                  }
              }
  
              if (kvp.Value.Previsiones.Count &gt; 0)
              {
                  var previsiones = kvp.Value.Previsiones.Where(prevision =&gt; prevision.Value.LPrevision[1] != null).ToList();
                  int previsionesCount = previsiones.Count;
  
                  for (int a = 0; a &lt; previsionesCount; a++)
                  {
                      var registros = previsiones[a].Value.LPrevision[1].Serie;
                      int c = registros.Count;
  
                      if (soloMejoresMetodos)
                      {
                          if (localKvp.Value.MejorMetodo != previsiones[a].Key) continue;
                          for (int i = 0; i &lt; c; i++)
                          {
                              var p = new Prediccion()
                                          {
                                              Id = articulo.Id,
                                              Nombre = articulo.Codigo,
                                              Descripcion = articulo.Descripcion,
                                              NombreMetodo =
                                                  Utils.SplitStringByCapitals(previsiones[a].Value.NombreMetodo),
                                              Fecha = registros[i].Fecha,
                                              PrediccionArticulo = Math.Round(registros[i].Cantidad, 2),
                                              EsMejorMetodo =
                                                  (previsiones[a].Value.NombreMetodo == localKvp.Value.MejorMetodo)
                                                      ? true
                                                      : false
                                          };
  
                              // This line experiences performance loss
                              prediccionList.Add(p);
                          }
                      }
                      else
                      {
                          for (int i = 0; i &lt; c; i++)
                          {
                              prediccionList.Add(new Prediccion()
                                                     {
                                                         Id = articulo.Id,
                                                         Nombre = articulo.Codigo,
                                                         Descripcion = articulo.Descripcion,
                                                         NombreMetodo = previsiones[a].Value.NombreMetodo,
                                                         Fecha = registros[i].Fecha,
                                                         PrediccionArticulo =
                                                             Math.Round(registros[i].Cantidad, 2),
                                                         EsMejorMetodo =
                                                             (previsiones[a].Value.NombreMetodo ==
                                                              localKvp.Value.MejorMetodo)
                                                                 ? true
                                                                 : false
                                                     });
                          }
                      }
                  }
              }
              else
              {
                  prediccionList.Add(new Prediccion()
                                         {
                                             Id = articulo.Id,
                                             Nombre = articulo.Codigo,
                                             Descripcion = articulo.Descripcion,
                                             NombreMetodo = kvp.Value.ErroresDatos[0].Texto,
                                         });
              }
          }
      }
  </code></pre>
  
  <hr>
  
  <p>Small description of the method:
  - the method reads an object (a concurrent dictionary) and updates a list (in this case a LinkedList) with the forecasts corresponding to a certain article. </p>
  
  <p>The concurrent dictionary object is constantly updated from various threads that access it concurrently.</p>
  
  <p>The list is initialized with null predictions corresponding to all the articles; thus, for instance, if you have 700 articles, in the beginning the list will be populated with 700 blank forecasts.</p>
  
  <p>As the concurrent dictionary is updated by one of the computing threads, it raises an event which calls the method mentioned above, which at its turn, updates the list (prediccionList).</p>
  
  <p>The maximum number of records which could be hold in the prediccionList (in this case) is about 500000 records, but the loss in performance could be noticed after adding some 40000 records in the list.</p>
  
  <p>The code might seem a bit rusty, as I have tried various optimizations tricks (replace the foreach'es with for's, calculate the count's outside the loops, replace the List&lt;> object with a LinkedList&lt;> etc.). Finally I reached the conclusion that the part that slows down the execution time is the line "prediccionList.Add(p);".</p>
  
  <p>The objects that are added to the list are instances of the Prediccion class; this object I consider not to be very heavy, it only contains 7 fields.</p>
  
  <h1>Memory usage</h1>
  
  <p>I attach the result from a memory profiling. The memory used doesn't surpass 256 MB, thus I don't believe the memory should be a problem here.<img src="https://i.stack.imgur.com/hIXd4.jpg" alt="enter image description here"></p>