Monday, 6 May 2013

Benchmarking Different Data Structures - Part Two (List vs Queue vs LinkedList)


Introduction

This is part two of the benchmarking article. In this article, I will use the BenchHelper class library I made in part one to benchmark the performance of various data structures and draw a conclusion.

The reason why I wish to conduct this experiment is because in most projects I use a list due to my familiarity with it and it's ease of use. However, from what I have read, a list may not be ideal for all scenarios. So by  becoming more familiar with other data structures and knowing their performance implications; I can make reliable decisions in which to use in the future.

For these tests, I wish to create benchmarks for adding a number of items to the end of a collection and also for removing from the beginning of a collection. Consider this as a FIFO experiment between different methods.

The results and conclusions in this article should not be taken as defining reasons for picking a data structure over another, as there are many different reasons that will influence your decision. These tests compare simple adding and removal scenarios, but there are other (more efficient ways) to use these data structures.

These experiments were run on a machine with the following spec:
  • Intel Pentium Dual-Core T2310 1.46GHz
  • 2GB memory
  • Windows 7 Professional

Data Structures

Below is an overview of the data structures I will use for this experiment:

List
A list is a non-fixed sized collection. It provides methods for adding, inserting and removing items in the collection with an index. An internal array is used which doubles in size once the size of the array has been reached. I use this collection data type more than any other due to it's ease of use.

Queue
A queue is a first-in first-out (FIFO) collection data structure. This means that the first item you store in the queue will be the first item that is retrieved from the queue. Queue provides no methods for accessing items via an index. A stack implements LIFO which is opposite to the queue.

Linked List
A doubly linked list is a data structure that contains a node which stores a value and a reference to the next and previous node. This provides a memory advantage because the references to a node are only known when a node that references it is accessed.

Methods

Below shows my main method for this program. This is explained in part one.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
 private static void DoMultiRunTests()
        {
            var numberOfTestRuns = 100;
            var loopTest = new LoopTest(sizeOfCollection);
            var test = new BenchmarkTest(loopTest);

            PrintTest(String.Format("**Average of adding {0} items to a LinkedList. Test has been executed {1} times**", sizeOfCollection, numberOfTestRuns),
                test.DoTest(AddToLinkedList, numberOfTestRuns, ClearLinkedList));
            PrintTest(String.Format("**Average of adding {0} items to a List. Test has been executed {1} times**", sizeOfCollection,numberOfTestRuns),
                test.DoTest(AddToList, numberOfTestRuns, ClearList));
            PrintTest(String.Format("**Average of adding {0} items to a Queue. Test has been executed {1} times**", sizeOfCollection,numberOfTestRuns), 
                test.DoTest(AddToQueue, numberOfTestRuns, ClearQueue));

            PrintTest(String.Format("**Average of removing {0} items from a LinkedList. Test has been executed {1} times**", sizeOfCollection, numberOfTestRuns),
                test.DoTest(RemoveFromLinkedList, numberOfTestRuns, ResetLinkedList));
            PrintTest(String.Format("**Average of removing {0} items from a List. Test has been executed {1} times**", sizeOfCollection, numberOfTestRuns),
                test.DoTest(RemoveFromList, numberOfTestRuns, ResetList));
            PrintTest(String.Format("**Average of removing {0} items from a Queue. Test has been executed {1} times**", sizeOfCollection,numberOfTestRuns),
                test.DoTest(RemoveFromQueue, numberOfTestRuns, ResetQueue));
        }

Below shows the collection methods I have used for populating and removing from a collection. I pass these methods as an Action to the BenchmarkHelper.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
        private static void AddToList(int i)
        {
            list.Add(i);
        }

        private static void AddToQueue(int i)
        {
            queue.Enqueue(i);
        }

        private static void AddToLinkedList(int i)
        {
            linkedList.AddLast(i);
        }

        private static void RemoveFromList(int i)
        {
            list.Remove(i);
        }

        private static void RemoveFromQueue()
        {
            queue.Dequeue();
        }

        private static void RemoveFromLinkedList()
        {
            linkedList.RemoveFirst();
        }

Results

Below shows a table of the results in milliseconds:

                                           Number of items added to the queue
Method 1000 5000 10000 15000 20000
list.Add 0.02993 0.155719 0.351502 0.423189 0.559053
queue.Enqueue 0.03525 0.387643 0.314305 0.460395 0.614273
linkedlist.AddLast 0.12743 0.558551 1.171819 2.131805 2.454959

                                           Number of items removed from the queue
Method 1000 5000 10000 15000 20000
list.Remove 0.821567 13.926008 53.429491 128.310909 224.46324
queue.Dequeue 0.031785 0.149658 0.3079 0.436147 0.580466
linkedlist.RemoveFirst 0.047381 0.231816 0.47043 0.71891 0.882717

Graphs

Below shows a graph visually comparing the speeds:
This graph shows that the amount of time to add items to a linked list increases rapidly after 5000 items, whereas the queue and list maintain a slight incline. In fact, the amount of time it takes to add items to a queue and list at 20000 items takes a similar amount of time to adding items to a linked list at 5000 items.

Possible reasons for this is that a linked list creates a new memory allocation for each added item, whereas a list uses an internal array which exists under a single memory block - this suggests that the time it takes to create a new node reference takes longer than accessing and recreating an internal array. Also, as the list grows bigger, the number of times a list needs to recreate the array will decrease because the capacity will double in size meaning it needs more items to exceed the limit.

From what I have read in the past, a linked list only provides a performance advantage when you need to access a location in the middle of the collection. However, in the case of adding an item to the end of a collection - a linked list is not satisfactory and I would prefer to use a queue or list.


This graph shows a very large performance difference between the list.Remove implementation and the other remove methods. There is only a slight difference between these methods at the 5000 item mark, but beyond this the list.Remove method performance drops considerably.

I appreciate this may not be the ideal approach to remove from a list as there are alternative methods such as RemoveAll but I haven't yet experimented with this (perhaps you can). However, this experiment clearly shows that using list.Remove for removing 5000+ items is definitely not the approach to use.



This graph is a closeup of the previous graph but for comparing the queue and linkedlist. Both of these methods perform well and are steady and reliable. The only conclusion I can make is that a linkedList is slightly faster.

Conclusion

The purpose of this experiment was to test two specific scenarios - adding to the end of a collection and removing from the start of a collection (FIFO). 

The adding to a collection experiment had clearly shown that when adding over 5000 items to a collection, the linkedList.AddLast method should not be used. It had taken 2.45 ms to add 20000 items whereas the queue had taken 0.6 ms and the list had taken 0.5 ms. You are likely to not notice the difference in the running of the application, but if are to add a larger number of items to a linked list then the performance affect may become more noticeable.

The removing from a collection experiment has revealed that the list.Remove method is super unreliable and should not be used with a collection size of more than 5000 items. I have read that the RemoveAll method is a much faster solution and would possibly more complete in this scenario - so use that instead!

It appears that for this particular experiment; the queue is the best overall performer here - which isn't a surprise seeing as it is specifically a FIFO implementation. The list.Remove had poorly performed in removing large amounts of data and the linkedlist.AddLast had under performed in adding large amount of data.

No comments:

Post a Comment