WiseOwl Training - Established 1992 Wise Owl Training

Established May 1992
30 years in business
Wise Owl Training
30 years in business
See 519 reviews for our classroom and online training
If you found this blog useful and youâ€™d like to say thanks you can click here to make a contribution. Thanks for looking at our blogs!

BLOGS BY TOPIC

BLOGS BY AUTHOR

BLOGS BY YEAR

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.

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

'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

'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