BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
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.
- VB.NET Data Storage Types Compared and Benchmarked
- VB.NET Benchmarking Test of speeds of data structures
- Arrays - Visual Basic data structures
- ArrayLists and SortedLists - Visual Basic data structures (this blog)
- Dictionaries and HashTables - Visual Basic data structures
- Lists - Visual Basic data structures
- Using data tables - Visual Basic data structures
- 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.
- VB.NET Data Storage Types Compared and Benchmarked
- VB.NET Benchmarking Test of speeds of data structures
- Arrays - Visual Basic data structures
- ArrayLists and SortedLists - Visual Basic data structures (this blog)
- Dictionaries and HashTables - Visual Basic data structures
- Lists - Visual Basic data structures
- Using data tables - Visual Basic data structures
- Benchmark Results and Recommendations