Vb.Net 2D Dictionary - very slow -
i need create 2d dictionary/keyvalue pair. tried this.
dim twodimdata new dictionary(of string, dictionary(of string, string)) 'create empty table each aid in aidlist '(contains 15000 elements) twodimdata.add(aid, new dictionary(of string, string)) each bid in bidlist 'contains 30 elements twodimdata.item(aid).add(bid, "") next next 'later populate values. [some code here populate table] 'now access value 'the idea access info given below (access row name & col name) msgbox twodimdata.item("a004").item("b005") ' should give value of 2 msgbox twodimdata.item("a008").item("b002") ' should return empty string. no error
issue:
the issue in creating empty table. takes 70 seconds create twodimdata table empty values. else seems fine. there way improve performance - may instead of using dictionary?
i suggest try dictionary(of tuple(of string, string), string)
instead. is, keys pairs of strings (tuple(of string, string)
) , values strings. appear correspond nicely diagram in question.
dim matrix new dictionary(of tuple(of string, string), string) ' add value matrix: matrix.add(tuple.create("a003", "b004"), "3") ' retrieve value matrix: dim valueata003b004 = matrix(tuple.create("a003", "b004"))
of course can define own key type (representing combination of 2 strings) if tuple(of string, string)
seems generic taste.
alternatively, use (possibly jagged) 2d arrays, potentially waste lot of space if data sparse (i.e. if there many empty cells in 2d matrix); , you'd forced use numeric indices instead of strings.
p.s.: actually, consider changing dictionary value type string
integer
; example matrix suggests contains integer numbers, might not make sense store them strings.
p.p.s.: not add values "empty" cells dictionary. wasteful. instead, instead of retrieving value dictionary, check whether dictionary contains key:
dim valuea string = "" ' default value if matrix.trygetvalue(tuple.create("a007", "b002"), valuea) ' given key present, , associated value has been retrieved … end if
Comments
Post a Comment