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
- Dictionaries and HashTables - Visual Basic data structures (this blog)
- 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.
Dictionaries and HashTables
Dictionaries and hashtables are similar: they both store information in key/value pairs (exactly what we want to do for our example).
Dictionary
Here is the code we use to test dictionary access:
Sub DictionaryTest()
Dim d As New Dictionary(Of String, Integer)
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
d.Add(TestCode(i), i)
Next
'work out how long this took
WriteTime = ElapsedTime
'store ten values for subsequent tests
StoreTenValues()
'sort the dictionary (get list of keys first)
Dim keys As List(Of String) = d.Keys.ToList
keys.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 = d.Keys(i)
Next
'report how long this took
Read1Time = ElapsedTime
'fourth test: find 10 "random" values by name
For i = 0 To 9
s = d.Item(TextValues(i))
Next
Read2Time = ElapsedTime
'write out results of test
AddRow("Dictionary")
End Sub
HashTable
The code I used to test hashtable access is as follows:
Sub TestHashTable()
Dim ht As New Hashtable
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
ht(i) = TestCode(i)
Next
'work out how long this took
WriteTime = ElapsedTime
'store ten values for subsequent tests
StoreTenValues()
'hash tables don't support native sorting
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 = ht.Keys(i)
Next
'report how long this took
Read1Time = ElapsedTime
'fourth test: find 10 "random" values by name
For i = 0 To 9
s = ht(TextValues(i))
Next
Read2Time = ElapsedTime
'write out results of test
AddRow("Hash Table")
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
- Dictionaries and HashTables - Visual Basic data structures (this blog)
- Lists - Visual Basic data structures
- Using data tables - Visual Basic data structures
- Benchmark Results and Recommendations