HaMeD
HaMeD

Reputation: 359

List<T> vs arrays, which one is faster?

I have an application that on start-up loads 10 List<myClass>. My work loads a little slow and I was thinking to use arrays instead of List<T>.

My Code:

    List<MyClass> objects = MyClass.Get();
    foreach (MyClass c in objects)
    {
        comboBox.Items.Add(c.Text);
    }


    class MyClass
    {

        //some properties

        public MyClass(string key)
        {
             //Database is another class for executing sql queries.
             //GetString(int columnIndex, int rowIndex)
             using (DataBase db = new DataBase())
             {
                 db.Query = "SELECT ID FROM sampleTable WHERE ID= @param";
                 db.Parameters.Add("param", SqlDbType.NVarChar, 100, key);
                 Name = db.GetString(0, 0);
             }
        }

        public static List<MyClass> Get()
        {
             using (Database db = new Database())
             {
                 db.Query = "SELECT ID FROM sampleTable ORDER BY sampleTextField";
                 int c = db.GetRowCount();
                 List<MyClass> result = new List<MyClass>();
                 for (int i = 0; i < c; i++)
                 {
                     result.Add(new MyClass(db.GetString(0, i)));
                 }
                 return result;
             }
         }
    }

Is it a good idea to use arrays?

Please feel free to offer me any idea for speeding up this code.

Upvotes: 1

Views: 313

Answers (5)

Guillaume
Guillaume

Reputation: 13138

List internaly uses an array but when properly used, the overhead is low.

In your code, you could pre-allocate the required capacity :

             int c = db.GetRowCount();
             List<MyClass> result = new List<MyClass>(c); //capacity
             for (int i = 0; i < c; i++)
             {
                 result.Add(new MyClass(db.GetString(0, i)));
             }

Anyway, you have to profile your code to be sure on what is taking time.

Upvotes: 0

to StackOverflow
to StackOverflow

Reputation: 124696

I doubt using a List is a bottleneck. With an array you would need to know the size in advance so you can dimension the array properly:

             MyClass[] result = new MyClass[c];
             for (int i = 0; i < c; i++)
             {
                 result[i] = new MyClass(db.GetString(0, i));
             }

But if you know the size in advance, you can initialize your list to the requierd capacity in the same way, thus avoiding reallocations and giving almost the same performance:

             List<MyClass> result = new List<MyClass>(c);
             for (int i = 0; i < c; i++)
             {
                 result.Add(new MyClass(db.GetString(0, i)));
             }

However I would reiterate that this is unlikely to make much difference.

Upvotes: 0

jaket
jaket

Reputation: 9341

Since you know the final size of your list, you could surely speed things up by setting the list capacity at construction time.

List<MyClass> result = new List<MyClass>(c);

Doing so would avoid reallocations of the underlying array. Otherwise, I don't think you really have any more significant overhead.

Upvotes: 0

Jack0x539
Jack0x539

Reputation: 626

Test it with arrays, and time it! I've done something similar recently and found that generally arrays were very slightly faster, but coding for lists is generally easier. C# is good at lists, unlike C++ where I used to find that arrays gave larger benefits over vectors.

Upvotes: 1

Matthew Watson
Matthew Watson

Reputation: 109557

You would have to use something like a Stopwatch to time it to be sure.

However, I would guess that it will make practically no difference because the times will be completely swamped by the overhead of the database access.

Therefore you should focus on speeding up the database access rather than worrying about using arrays or lists. But before you do anything else, instrument your program (using Stopwatch) so that you know where the time is going.

Upvotes: 2

Related Questions