Review of VB.NET Data Storage Structures
Part seven 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
  5. Dictionaries and HashTables - Visual Basic data structures
  6. Lists - Visual Basic data structures
  7. Using data tables - Visual Basic data structures (this blog)
  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.

Data Tables

Data tables aren't really a way of storing name/value pairs for quick access, but instead a way of storing data from a relational database in memory.  I'd expect them to perform badly in these benchmarking tests (we shall see!).

 

My code starts by importing the appropriate namespace:

'make sure can refer to Data objects

Imports System.Data

I then create an enumeration:

Enum enumType

StringType = 1

BooleanType = 2

DateTimeType = 3

DecimalType = 4

DoubleType = 5

IntegerType = 6

CharType = 7

End Enum

The code then creates a data table with two columns: an Id column (containing a number) and a Value column containing a string of text.

Sub DataTableTest()

Dim dtTest As New DataTable

Dim dr As DataRow

Dim dc1 As DataColumn = New DataColumn("Id")

dc1.DataType = System.Type.GetType("System.Int32")

dtTest.Columns.Add(dc1)

Dim dc2 As DataColumn = New DataColumn("Value")

dc2.DataType = System.Type.GetType("System.String")

dtTest.Columns.Add(dc2)

The rest of the code to test time taken to write, sort and read values is as follows:

StartTimer()

Dim i As Integer

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

dr = dtTest.NewRow

dr("Id") = i

dr("Value") = TestCode(i)

dtTest.Rows.Add(dr)

Next

'work out how long this took

WriteTime = ElapsedTime

'store ten values for subsequent tests

StoreTenValues()

'sort using dataview

Dim dvTest As DataView = dtTest.DefaultView

dvTest.Sort = "Value"

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))

For Each dr In dtTest.Rows

If Convert.ToInt32(dr("Id")) = i Then

Exit For

End If

Next

Next

'report how long this took

Read1Time = ElapsedTime

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

For i = 0 To 9

s = TextValues(i)

For Each dr In dtTest.Rows

If Convert.ToString(dr("Value")) = s Then

Exit For

End If

Next

Next

Read2Time = ElapsedTime

'write out results of test

AddRow("Data table")

End Sub

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

This blog has 0 threads Add post