Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In some article i read that arrays are faster than generic list, but i got different results (or i was wrong).

What I have tried:

C#
static int[] brojevi;

        static List<int> mojaLista = new List<int>(10000);// when capacity not specified elapsed time is 00:00:00.0000199
        
        private void button_Click(object sender, RoutedEventArgs e)
        {
            ////// using array //////////
            //stopWatch.Start();
            //for (int i = 0; i < brojevi.Length - 1; i++)
            //{
            //    i = i + 1;                                  // elapsed time is 00:00:00.0000221
            //}
            //stopWatch.Stop();
            //TimeSpan ms = stopWatch.Elapsed;
            //MessageBox.Show(ms.ToString());

            ////// using list<int> ///////
            stopWatch.Start();
            for (int i = 0; i < mojaLista.Count; i++)
            {
                i = i + 1;                                      // elapsed time is 00:00:00.0000213
            }
            stopWatch.Stop();
            TimeSpan ms = stopWatch.Elapsed;
            MessageBox.Show(ms.ToString());
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //brojevi = new int[10000];
            //for (int i = 0; i < 10000; i++)
            //{
            //    brojevi[i] = i;
            //}

            for (int i = 0; i < 10000; i++)
            {
                mojaLista.Add(i);
            }
        }
Posted
Updated 9-Feb-20 5:07am
v4

List will always be slower than an array.

Your Button_Click method where most of the code is commented out doesn't do anything at all with an array or a list, unless you're testing the Length and Count properties. That's not really a good test.

The reason List will always be slower is because of how a List works. Internally, it maintains an array. It's managed by the methods exposed by List and those methods add overhead to the execution.

For example, to put an item in an array, you simply use an index into the array and set that index to whatever you're adding. The Add method of the array maintains the next available index itself, but has to check to see if the next index is still without the bounds of the array it has allocated. If it's not, a new array has to be allocated and the objects in the old array moved over to it.
 
Share this answer
 
Comments
Gruja82 9-Feb-20 11:14am    
You're right i changed i=i+1 with testLista.Add(i) and results are when using Array - 00:00:00.0000803, and using list<int> - 00:00:00.0002224.
As Dave says: an Array will always be faster than a List, simply because a List is array with a wrapper class: List<T> - Is it really as efficient as you probably think?[^]
 
Share this answer
 
Quote:
In some article i read that arrays are faster than generic list, but i got different results (or i was wrong).

You are wrong!
The problem is that your code don't try to access list or array.
Your code is optimized by compiler:
C#
stopWatch.Start();
// because the loop to not do anything with the list, the count is handled as a constant of the loop and accessed once.
int Tmp= mojaLista.Count;
for (int i = 0; i < Tmp; i++)
{
    i = i + 1; // this is only adding 1 to the loop counter
}

and can be:
C#
stopWatch.Start();
int Tmp= mojaLista.Count;
for (int i = 0; i < Tmp; i+=2)
{
}

Nothing can beat an array on read/write.
A list is faster than array when you need to resize (add or remove elements).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900