Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating speed and data type

Does the iteration speed through an array (or list,linkedList,Dictionary ect) depend on the data type?

Example: An array of 10 bools v/s an array of 10 integers?

like image 563
orukinawa Avatar asked May 11 '13 05:05

orukinawa


2 Answers

Yes, the datatype matters. It has nothing to do with the iteration; it has everything to do with the datatypes.

Value types

An int is 4 bytes in length. A decimal is 16 bytes in length. So a decimal is 4 times bigger than an int. Every time you retrieve a value from the array the that value is copied. In case of a decimal al 16 bytes are copied. (In case of a reference type the reference is copied, normally 4 or 8 bytes). Copying more bytes will simply slow down the iteration.

Boxing

If you iterate trough a collection, there is also the possibility that you have change type. For example:

foreach(object o in new int[] { 1,2,3 })
     ....

This will box every int to an object. This takes time. That has nothing to do with the iteration, it has everything to do with the fact that you are boxing.

Casting

Last example: There are also arrays in where you have to cast:

foreach(Person p in new object[] { ... })
     ....

Casting also takes extra time.

EDIT

Some time measurements to backup my claim:

Times in milliseconds. Arrays are of size 10,000. Iterations also 10,000.

like image 113
Martin Mulder Avatar answered Sep 21 '22 07:09

Martin Mulder


Run the code below if you want , but here's a quick comparison. All that it does is iterate over the array/list, and set a temp variable to the value in that index.

3rd run, high priorityThrew some more types in

Do note that somehow the Int performance took a hit when running now ... no idea why ... but it happens on repeated runs as well ...

    namespace Iterating_types
    {
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
class Program
    {
        static void Main(string[] args)
        {
            Thread.CurrentThread.Priority = ThreadPriority.Highest;
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;

            Stopwatch watch = new Stopwatch();
            int UPPER = 1000000;
            int[] int_arr = Enumerable.Range(1, UPPER).ToArray();
            List<int> int_list = Enumerable.Range(1, UPPER).ToList();

            Int32[] int32_arr = Enumerable.Range(1, UPPER).ToArray();

            Int64[] int64_arr = new Int64[UPPER]; 

            IntObject[] intobject_arr = new IntObject[UPPER];
            List<IntObject> intobject_list = new List<IntObject>();

            string[] string_arr = new string[UPPER];
            List<string> string_list = new List<string>();

            bool[] bool_arr = new bool[UPPER];
            Boolean[] boolean_arr = new Boolean[UPPER];
            List<bool> bool_list = new List<bool>();
            List<Boolean> boolean_list = new List<Boolean>();
            // Initializing some of the above
            for (int i = 0; i < UPPER; i++)
            {
                int64_arr[i] = (Int64) i;
                string_arr[i] = i.ToString();
                string_list.Add(i.ToString());
                intobject_arr[i] = new IntObject(i);
                intobject_list.Add(new IntObject(i));
                bool_arr[i] = (i%2 ==0);
                boolean_arr[i] = (i%2 ==0);
                bool_arr[i] = (i%2 ==0);
                bool_list.Add(i%2 ==0);

                boolean_list.Add(i%2 == 0);
            }

            Console.WriteLine("Iterations: {0}{1}", UPPER, Environment.NewLine);
            Console.WriteLine("Thread priority: {0}", Thread.CurrentThread.Priority);
            Console.WriteLine("Process priority: {0}", Process.GetCurrentProcess().PriorityClass);

            Console.WriteLine("\n\rArrays:\t----------------------------------------------");

            bool b;
            b = bool_arr[1];
            watch.Start();
            for (int i = 0; i < UPPER; i++)
            {
                b = bool_arr[i];
            }
            watch.Stop();
            Console.WriteLine("Type: bool\tStructure: Array\tticks: {0}\tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);

            watch.Start();
            for (int i = 0; i < UPPER; i++)
            {
                b = boolean_arr[i];
            }
            watch.Stop();
            Console.WriteLine("Type: Boolean\tStructure: Array\tticks: {0}\tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);

            int temp_int;
            temp_int = int_arr[1];
            watch.Start();
            for (int i = 0; i < UPPER; i++)
            {
                temp_int = int_arr[i];
            }
            watch.Stop();
            Console.WriteLine("Type: Int\tStructure: Array\tticks: {0}\tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);

            Int32 temp_int32 ;
            temp_int32 = int32_arr[1];
            watch.Reset();
            watch.Start();
            for (int i = 0; i < UPPER; i++)
            {
                temp_int32 = int32_arr[i];
            }
            watch.Stop();
            Console.WriteLine("Type: Int32\tStructure: Array\tticks: {0}\tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);

            Int64 temp_int64 ;
            temp_int64 = int64_arr[1];
            watch.Reset();
            watch.Start();
            for (int i = 0; i < UPPER; i++)
            {
                temp_int64 = int64_arr[i];
            }
            watch.Stop();
            Console.WriteLine("Type: Int64\tStructure: Array\tticks: {0}\tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);

            string s ;
            s = string_arr[1];
            watch.Reset();
            watch.Start();
            for (int i = 0; i < UPPER; i++)
            {
                s = string_arr[i];
            }
            watch.Stop();
            Console.WriteLine("Type: string\tStructure: Array\tticks: {0}\tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);

            temp_int = intobject_arr[1].IntValue;
            watch.Reset();
            watch.Start();
            for (int i = 0; i < UPPER; i++)
            {
                temp_int = intobject_arr[i].IntValue;
            }
            watch.Stop();
            Console.WriteLine("Type: IntObject\tStructure: Array\tticks: {0}\tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);

            Console.WriteLine("\n\rLists:\t----------------------------------------------");

            watch.Reset();
            watch.Start();
            foreach (var val in bool_list)
            {
                b = val;
            }
            watch.Stop();
            Console.WriteLine("Type: bool\tStructure: List\t\tticks: {0}\tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);

            watch.Reset();
            watch.Start();
            foreach (var val in boolean_list)
            {
                b = val;
            }
            watch.Stop();
            Console.WriteLine("Type: Boolean\tStructure: List\t\tticks: {0}\tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);

            temp_int = int_list.First();
            watch.Reset();
            watch.Start();
            foreach (var val in int_list)
            {
                temp_int = val;
            }
            watch.Stop();
            Console.WriteLine("Type: Int\tStructure: List\t\tticks: {0}\tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);

            s = string_list.First();
            watch.Reset();
            watch.Start();
            foreach (var val in string_list)
            {
                s = val;
            }
            watch.Stop();
            Console.WriteLine("Type: string\tStructure: List\t\tticks: {0}\tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);

            temp_int = intobject_list.First().IntValue;
            watch.Reset();
            watch.Start();
            foreach (var val in intobject_list)
            {
                temp_int = val.IntValue;
            }
            watch.Stop();
            Console.WriteLine("Type: IntObject\tStructure: List\t\tticks: {0}\tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);

            Console.WriteLine();
            Console.WriteLine("Hit any key to exit.");
            Console.ReadKey();


        }
    }

    class IntObject
    {
        public int IntValue { get; set; }

        public IntObject ()
        {
            IntValue = 0;
        }

        public IntObject(int i)
        {
            IntValue = i;
        }
    }
}
like image 28
Noctis Avatar answered Sep 21 '22 07:09

Noctis