Review of VB.NET Data Storage Structures
Part four of an eight-part series of blogs

There are a bewildering array (excuse the pun) of data storage structures available to you in Visual Basic. Choose from arrays, ArrayLists, SortedLists, Dictionaries, HashTables, Lists and DataTables, among others. This blog gives an example of each type of structure, and benchmarks them to show which perform best and worst.

  1. VB.NET Data Storage Types Compared and Benchmarked
  2. VB.NET Benchmarking Test of speeds of data structures
  3. Arrays - Visual Basic data structures
  4. ArrayLists and SortedLists - Visual Basic data structures (this blog)
  5. Dictionaries and HashTables - Visual Basic data structures
  6. Lists - Visual Basic data structures
  7. Using data tables - Visual Basic data structures
  8. Benchmark Results and Recommendations

Posted by Andy Brown on 24 August 2011

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

ArrayLists and SortedLists

The main differences between arrays and arraylists are internal - the former stores information in an internal class (System.Array), while the latter uses a collection.

From the practical point of view the main difference is that it's easier to add items to an ArrayList in cases where  you don't know how many items you may be adding.

Here is the code I used to test the ArrayList:

Sub ArrayListTest()

Dim list As New ArrayList

StartTimer()

'first test: writing 10 ^ n values, increasing size of the array by 1 each time

Dim i As Integer

For i = 0 To (10 ^ RunSize) - 1

list.Add(TestCode(i))

Next

'work out how long this took

WriteTime = ElapsedTime

'store ten values for subsequent tests

StoreTenValues()

'sort the array list (timing this)

list.Sort()

SortTime = ElapsedTime

'third test: find 10 "random" values by number

Dim s As String

For i = (10 ^ RunSize) - 1 To 0 Step -(10 ^ (RunSize - 1))

s = list(i)

Next

'report how long this took

Read1Time = ElapsedTime

'fourth test: find 10 "random" values by name

For i = 0 To 9

s = TextValues(i)

'Note: using list.BinarySearch(s) doesn't seem to make much time difference

For j = 0 To (10 ^ RunSize) - 1

If list(j) = s Then

Exit For

End If

Next

Next

Read2Time = ElapsedTime

'write out results of test

AddRow("ArrayList", "ArrayList")

End Sub

I tried using the BinarySearch method to read data, but it didn't seem to make much difference to the times recorded.

SortedList

You can also use a SortedList, which ensures that the sort order of the structure is maintained for each new element that you add to it.  The code I used to test a SortedList is as follows:

Sub SortedListTest()

Dim list As New SortedList

StartTimer()

'first test: writing 10 ^ n values, increasing size of the array by 1 each time

Dim i As Integer

For i = 0 To (10 ^ RunSize) - 1

list.Add(i, TestCode(i))

Next

'work out how long this took

WriteTime = ElapsedTime

'store ten values for subsequent tests

StoreTenValues()

'sort the array list (timing this)

SortTime = 0

'third test: find 10 "random" values by number

Dim s As String

For i = (10 ^ RunSize) - 1 To 0 Step -(10 ^ (RunSize - 1))

s = list.GetByIndex(i).ToString

Next

'report how long this took

Read1Time = ElapsedTime

'fourth test: find 10 "random" values by name

For i = 0 To 9

s = list.GetByIndex(list.IndexOfValue(TextValues(i)))

Next

Read2Time = ElapsedTime

'write out results of test

AddRow("SortedList")

End Sub

The results of running these tests are shown at the end of this blog.

This blog has 0 threads Add post