Sign up Calendar Latest Topics
 
 
 


Reply
  Author   Comment  
Martin

Avatar / Picture

Administrator
Registered:
Posts: 2,230
Reply with quote  #1 
Hey all.
So continuing on from my last speed test (for loops vs foreach loops), I decided to test out the speed of arrays vs lists. I already knew that arrays were faster than lists, I believe because arrays are sequentially lined up in memory where as lists are scattered about; my knowledge of memory usage in code isn't amazing.

What I wanted to find out though was if converting a list to an array then looping over the array, was faster than looping over a list. And it was.
Here's the results:

Array vs list - 1.jpg 
SPEED

As you can see from the chart above. The speed from fastest to slowest goes:

1) Array
2) List converted to Array
3) List


LESSON LEARNT

When implementing a List, try converting it to an array first and see if you get better performance.


THE CODE


    private const int ELEMENT_COUNT = 1000;
    public float ArrayForEachTest()
    {
        //create the array
        int[] array = new int[ELEMENT_COUNT];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = i;
        }
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        foreach (int iNumber in array)
        {
            //just a basic operation
            int iNumberSquard = iNumber * iNumber;
        }
        stopwatch.Stop();
        //return stopwatch.ElapsedMilliseconds;
        return (float)stopwatch.Elapsed.TotalMilliseconds;
    }
    public float ListForeachTest()
    {
        //create the list
        List<int> list = new List<int>();
        for(int i = 0; i < ELEMENT_COUNT; i++)
        {
            list.Add(i);
        }
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        foreach (int iNumber in list)
        {
            //just a basic operation
            int iNumberSquard = iNumber * iNumber;
        }
        stopwatch.Stop();
        return (float)stopwatch.Elapsed.TotalMilliseconds;
    }
    public float ListToArrayForeachTest()
    {
        List<int> list = new List<int>();
        for (int i = 0; i < ELEMENT_COUNT; i++)
        {
            list.Add(i);
        }
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        int[] array = list.ToArray();
        foreach (int iNumber in array)
        {
            //just a basic operation
            int iNumberSquard = iNumber * iNumber;
        }
        stopwatch.Stop();
        return (float)stopwatch.Elapsed.TotalMilliseconds;
    }


Let me know if I've missed any glaring issues in the code. Will be good to discuss.

Happy Developing 😉
Martin


0
Martin

Avatar / Picture

Administrator
Registered:
Posts: 2,230
Reply with quote  #2 
Hey again all. I also did the test using a list / array with only 10 elements and the results were the same.

SPEED (fastest to slowest)

1) Array
2) List converted to Array
3) List


Array vs list - 2.jpg 

CODE (same as previous post, just element count changed)


    private const int ELEMENT_COUNT = 10;
    public float ArrayForEachTest()
    {
        //create the array
        int[] array = new int[ELEMENT_COUNT];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = i;
        }
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        foreach (int iNumber in array)
        {
            //just a basic operation
            int iNumberSquard = iNumber * iNumber;
        }
        stopwatch.Stop();
        //return stopwatch.ElapsedMilliseconds;
        return (float)stopwatch.Elapsed.TotalMilliseconds;
    }
    public float ListForeachTest()
    {
        //create the list
        List<int> list = new List<int>();
        for(int i = 0; i < ELEMENT_COUNT; i++)
        {
            list.Add(i);
        }
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        foreach (int iNumber in list)
        {
            //just a basic operation
            int iNumberSquard = iNumber * iNumber;
        }
        stopwatch.Stop();
        return (float)stopwatch.Elapsed.TotalMilliseconds;
    }
    public float ListToArrayForeachTest()
    {
        List<int> list = new List<int>();
        for (int i = 0; i < ELEMENT_COUNT; i++)
        {
            list.Add(i);
        }
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        int[] array = list.ToArray();
        foreach (int iNumber in array)
        {
            //just a basic operation
            int iNumberSquard = iNumber * iNumber;
        }
        stopwatch.Stop();
        return (float)stopwatch.Elapsed.TotalMilliseconds;
    }

0
Martin

Avatar / Picture

Administrator
Registered:
Posts: 2,230
Reply with quote  #3 
And, just to add yet another test. I tried the same as above but using a complex class type.
The results were the same:

SPEED (fastest to slowest)
1) Array.
2) List converted to Array.
3) List

Array vs list - 3 - complex types.jpg 

CODE TESTED

private const int ELEMENT_COUNT = 10;
    private class ComplexClass
    {
        public string m_sName = "SomeName";
        public int m_iSomeInt = 111;
        public double m_dSomeDouble = 2.222f;
    }
    public float ArrayComplexTypeForEachTest()
    {
        //create the array
        ComplexClass[] array = new ComplexClass[ELEMENT_COUNT];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = new ComplexClass();
        }
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        foreach (ComplexClass complexClass in array)
        {
            //just a basic operation
            int iNumber = complexClass.m_iSomeInt * complexClass.m_iSomeInt;
        }
        stopwatch.Stop();
        return (float)stopwatch.Elapsed.TotalMilliseconds;
    }
    public float ListComplexTypeForeachTest()
    {
        //create the list
        List<ComplexClass> list = new List<ComplexClass>();
        for (int i = 0; i < ELEMENT_COUNT; i++)
        {
            list.Add(new ComplexClass());
        }
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        foreach (ComplexClass complexClass in list)
        {
            //just a basic operation
            int iNumber = complexClass.m_iSomeInt * complexClass.m_iSomeInt;
        }
        stopwatch.Stop();
        return (float)stopwatch.Elapsed.TotalMilliseconds;
    }
    public float ListComplexTypeToArrayForeachTest()
    {
        List<ComplexClass> list = new List<ComplexClass>();
        for (int i = 0; i < ELEMENT_COUNT; i++)
        {
            list.Add(new ComplexClass());
        }
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        ComplexClass[] array = list.ToArray();
        foreach (ComplexClass complexClass in array)
        {
            //just a basic operation
            int iNumber = complexClass.m_iSomeInt * complexClass.m_iSomeInt;
        }
        stopwatch.Stop();
        return (float)stopwatch.Elapsed.TotalMilliseconds;
    }

0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.