Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding to List<t> becomes very slow over time

Tags:

c#

winforms

I'm parsing an html table that has about 1000 rows. I'm adding ~10 char string from one <td> in each row to a list<string> object. It's very quick for the first 200 or so loops but then becomes slower and slower over time.

This is the code i'm using:

List<string> myList = new List<string>();
        int maxRows = numRows;


        for (int i = 1; i < maxRows; i++)
        { 
            TableRow newTable = myTable.TableRows[i];
            string coll = string.Format("{0},{1},{2},{3},{4}",newTable.TableCells[0].Text,newTable.TableCells[1].Text,newTable.TableCells[2].Text,newTable.TableCells[3].Text,newTable.TableCells[4].Text);
            myList.Add(coll);
            label1.Text = i.ToString();
        }

Should I use an array instead?

Edit: I threw the above code in a new method that gets run on a new Thread and then updated my label control with this code:

label1.Invoke((MethodInvoker)delegate
                {
                    label1.Text = i.ToString();
                });

Program runs at a consistent speed and doesn't block the UI.

like image 241
The Muffin Man Avatar asked Nov 29 '22 18:11

The Muffin Man


1 Answers

If you roughly know the range (number of items) in your collection it is better to use an array.

Reason : Every time you add an element to the List if the list is full it allocates new block of memory to hold the double the current space and copies everything there and then keeps appending the additional entries till it becomes full, and one more allocation copy cycle.

Following is how it works AFAIK, start with 16 elements by default, when you add 17th element to the list it allocates 32 elemnts and copies 16 there then continues for 17 to 32. and repeats this process, so it is slower but offer flexibility of not having to determine the length beforehand. This might be the reason you're seeing the drag.

Thanks @Dyppl var list = new List<int>(1000); This is one elegant option too, as @Dyppl suggested it is best of both the worlds.

like image 146
Sanjeevakumar Hiremath Avatar answered Dec 10 '22 13:12

Sanjeevakumar Hiremath