Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vb.Net 2D Dictionary - very slow

Tags:

vb.net

I need to create a 2D dictionary/keyvalue pair. I tried something like this.

enter image description here

Dim TwoDimData As New Dictionary(Of String, Dictionary(Of String, String))

'Create an empty table
For Each aid In AIDList   '(contains 15000 elements)
    TwoDimData.Add(aid, New Dictionary(Of String, String))
    For Each bid In BIDList   'contains 30 elements
        TwoDimData.Item(aid).Add(bid, "")
    Next
Next

'Later populate values.
[some code here to populate the table]

'Now access the value
'The idea is to access the info as given below (access by row name & col name)
 Msgbox TwoDimData.Item("A004").Item("B005")   ' should give the value of 2
 Msgbox TwoDimData.Item("A008").Item("B002")   ' should return empty string. No error

Issue:

The issue is in Creating the empty table. It takes 70 seconds to create the TwoDimData table with empty values. Everything else seems to be fine. Is there any way to improve the performance - may be instead of using Dictionary?

like image 408
KitKarson Avatar asked Jan 01 '26 01:01

KitKarson


2 Answers

I suggest you try Dictionary(Of Tuple(Of String, String), String) instead. That is, the keys are pairs of strings (Tuple(Of String, String)) and the values are strings. That would appear to correspond nicely to the diagram in your question.

Dim matrix As New Dictionary(Of Tuple(Of String, String), String)

' Add a value to the matrix:
matrix.Add(Tuple.Create("A003", "B004"), "3")

' Retrieve a value from the matrix:
Dim valueAtA003B004 = matrix(Tuple.Create("A003", "B004"))

Of course you can define your own key type (representing a combination of two strings) if Tuple(Of String, String) seems too generic for your taste.

Alternatively, you could also just use (possibly jagged) 2D arrays, but that would potentially waste a lot of space if your data is sparse (i.e. if there are many empty cells in that 2D matrix); and you'd be forced to use numeric indices instead of strings.

P.S.: Actually, consider changing the dictionary value type from String to Integer; your example matrix suggests that it contains only integer numbers, so it might not make sense to store them as strings.

P.P.S.: Do not add values for the "empty" cells to the dictionary. That would be very wasteful. Instead, instead of simply retrieving a value from the dictionary, you check whether the dictionary contains the key:

Dim valueA As String = ""  ' the default value
If matrix.TryGetValue(Tuple.Create("A007", "B002"), valueA) Then
    ' the given key was present, and the associated value has been retrieved
    …
End If
like image 92
stakx - no longer contributing Avatar answered Jan 03 '26 14:01

stakx - no longer contributing


I would think that a simple structure would suffice for this?

Public Structure My2DItem
  Public Row As Integer
  Public Col As Integer
  Public Value As String
End Structure

Public My2DArray As Generic.List(Of My2DItem) = Nothing
Public Size As Integer
Public MaxRows As Integer
Public MaxCols As Integer
'
Sub Initialise2DArray()
'
Dim CountX As Integer
Dim CountY As Integer
Dim Item As My2DItem
'
  'initialise
  MaxRows = 15000
  MaxCols = 30
  Size = MaxRows * MaxCols
  My2DArray = New Generic.List(Of My2DItem)
  '   
  'Create an empty table
  For CountY = 1 To 15000
    For CountX = 1 To 30
      Item = New My2DItem
      Item.Row = CountY
      Item.Col = CountX
      Item.Value = "0"
      My2DArray.Add(Item)
      Item = Nothing
    Next
  Next
'
End Sub

And to read the data out of the array,

Function GetValue(Y As Integer, X As Integer) As String
'
Dim counter As Integer
'
GetValue = "Error!"
If My2DArray.Count > 0 Then
  For counter = 0 To My2DArray.Count - 1
    If My2DArray(counter).Row = Y Then
      If My2DArray(counter).Col = X Then
        GetValue = My2DArray(counter).Value
        Exit Function
      End If
    End If
  Next
End If
'
End Function

And to read your sample cell A004 B005

MyStringValue = GetValue(4,5)
like image 21
Zeddy Avatar answered Jan 03 '26 15:01

Zeddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!