Review of VB.NET Data Storage Structures
Part three 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 (this blog)
  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
  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.

Arrays

An array holds related data in numbered cells.  Thus an array of 3 friends' names might read:

  • Friends(0) = "Shadrach"
  • Friends(1) = "Meshach"
  • Friends(2) = "Abednego"

Arrays are always numbered from 0 in .NET.

In addition, you can have static and dynamic arrays:

Type of array Notes
Static In a static array, you specify the size of the array when you first create it, and it doesn't subsequently increase. 
Dynamic In a dynamic array, whenever you find a new value you increase the size of the array by 1 to accommodate it.

Here is the code I used to test the static array.  I've given it a fixed size of 100,000 elements, so it can accommodate lots of data.

Sub ArrayStatic()

Dim test(99999) As String

StartTimer()

'read values into static array

Dim i As Integer

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

test(i) = TestCode(i)

Next

'work out how long this took

WriteTime = ElapsedTime

'store ten values for subsequent tests

StoreTenValues()

'sort the array (timing this)

Array.Sort(test)

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 = test(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)

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

If test(j) = s Then

Exit For

End If

Next

Next

'this shows results on my internal site

AddRow("Array", "Static array (fixed size 10000)")

End Sub

The code to test a dynamic array is as follows:

Sub ArrayDynamic()

Dim test() As String = Nothing

StartTimer()

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

'(note the word PRESERVE to avoid losing previous contents)

Dim i As Integer

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

ReDim Preserve test(i)

test(i) = TestCode(i)

Next

'work out how long this took

WriteTime = ElapsedTime

'store ten values for subsequent tests

StoreTenValues()

'sort the array (timing this)

Array.Sort(test)

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 = test(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)

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

If test(j) = s Then

Exit For

End If

Next

Next

Read2Time = ElapsedTime

'write out results of test

AddRow("Array", "Dynamic array")

End Sub

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

This blog has 0 threads Add post