4

Possible Duplicate:
When should I use a List vs a LinkedList

This question is related to my earlier question which was merged: related to List vs LinkedList

If I expect not to use the access by index for my data structure how much do I save by using LinkedList over List ? If if am not 100% sure I will never use access by index, I would like to know the difference.

Suppose I have N instances. inserting and removing in a LinkedList will only be a o(1) op , where as in List it may me be O(n), but since it it optimized, it would be nice to know what the difference is for some values of n. say N = 1,000,000 and N = 1,000,000,000

Community
  • 1
  • 1
HCP
  • 912
  • 2
  • 10
  • 18

4 Answers4

6

OK I did this experiment and here is the result:

This is the elapsed ticks for a list and linked list with 1000,000 items:

LinkedList 500 insert/remove operations: 10171
List 500 insert/remove operations: 968465

Linked list is 100 times faster when compared for 1000,000 items.


Here is the code:

    static void Main(string[] args)
    {

        const int N = 1000*1000;
        Random r = new Random();
        LinkedList<int> linkedList = new LinkedList<int>();
        List<int> list = new List<int>();
        List<LinkedListNode<int>> linkedListNodes = new List<LinkedListNode<int>>();

        for (int i = 0; i < N; i++)
        {
            list.Add(r.Next());
            LinkedListNode<int> linkedListNode = linkedList.AddFirst(r.Next());
            if(r.Next() % 997 == 0)
                linkedListNodes.Add(linkedListNode);
        }

        Stopwatch stopwatch = new Stopwatch();

        stopwatch.Start();
        for (int i = 0; i < 500; i++)
        {
            linkedList.AddBefore(linkedListNodes[i], r.Next());
            linkedList.Remove(linkedListNodes[i]);
        }
        stopwatch.Stop();
        Console.WriteLine("LinkedList 500 insert/remove operations: {0}", stopwatch.ElapsedTicks);
        stopwatch.Reset();

        stopwatch.Start();
        for (int i = 0; i < 500; i++)
        {
            list.Insert(r.Next(0,list.Count), r.Next());
            list.RemoveAt(r.Next(0, list.Count));
        }
        stopwatch.Stop();
        Console.WriteLine("List 500 insert/remove operations: {0}", stopwatch.ElapsedTicks);


        Console.Read();
    }
}
Aliostad
  • 76,981
  • 19
  • 152
  • 203
4

Don't worry about it. Write your app as you would normally. When it's finished, run it a few times with real data and profile the performance. Swap out the class you're using and compare results.

Joe
  • 42,600
  • 24
  • 134
  • 225
3

List uses an array internally, which always has a fixed size, for example it uses size of 16 (usually power of two), so even if you store one item, it will hold array with size 16 with 15 being empty.

Now assume, When you store all 16 items and when you try to add 17th item, it will now create a new array of 32, (16 + 16), copy all 16 from old array to new array, and then put item on 17th place in array of 32 size.

Now, when you remove an item from 1st place, all items after 1st item, are moved to one step ahead, so 2nd becomes 1st, 3rd becomes 2nd etc etc.

Where else Linked List is made out of pointers, it is not array, it only occupies nodes that you create, and it stores references of its previous and next nodes.

For performance issues, List will work great if you are going to add/remove items very rarely but you will be iterating (reading entire list, or accessing items via index) very frequently. List will be better for random access scenarios, like you will be able to get items very fast with index specified.

Linked list will work great if you are going to add/remove items very frequently, but you will be iterating it (entire list) rarely. Linked list will be more appropriate for sequential access, where you will navigate back or front from current node, but it will be of very poor performance when you will try to find indexed items.

Akash Kava
  • 37,127
  • 20
  • 114
  • 162
0

Back-inserting in a List<T> actually runs in amortized constant time. However, removing elements is expensive (O(n)).

Community
  • 1
  • 1
Hosam Aly
  • 38,883
  • 35
  • 132
  • 179