Hi
I'm trying to create something like a MultiKeyDictionary/List while maintaining the fastest possible speed.
It is almost finished but one problem remains when adding elements:
If I use a List Object the adding of elements will be fast but creating a new instance of it is slow,
on the other hand creating a Integer array is fast but redimensioning is really slow.
Here the Code in question:
Is there a way to speed up the redimensioning of the Integer array?
I already tried the ObjectModel.Collection(Of Integer) Object and it is a little faster than List(Of Integer),
but still while adding it takes almost twice the time it takes with an Integer array.
For comparsion:
Using just an Integer (No array therefore no duplicate Key possible):
Adding 2million Object => 3812.5 ms
Using Integer array (duplicate Keys possible):
Every key unique, 2m objects => 4968.75 ms
Always same key, 2m obj => aborted >2min
Using ObjectModel.Collection(Of Integer) (...):
Every key unique, 2m obj => 8421.875 ms
Always same key, 2m obj => 2125.125 ms
Using List(Of Integer) (...):
Every key unique, 2m obj => 7437.5 ms
Always same key, 2m obj => 2125.125 ms
I'm trying to create something like a MultiKeyDictionary/List while maintaining the fastest possible speed.
It is almost finished but one problem remains when adding elements:
If I use a List Object the adding of elements will be fast but creating a new instance of it is slow,
on the other hand creating a Integer array is fast but redimensioning is really slow.
Here the Code in question:
Visual Basic:
Public Sub Add(ByVal Value As tValue, ByVal KeyIndeces As iKeyIndeces)
Dim I As Integer
Dim KeyIndex As Object
Dim Indeces As List(Of Integer) ' indeces Container for duplicate Keys
For Each Key As Dictionary(Of Object, List(Of Integer)) In Keys.Values
KeyIndex = KeyIndeces.GetKey(I, Value)
If Not Key.ContainsKey(KeyIndex) Then 'Add Key, Index to Dict
Indeces = New List(Of Integer) '####Slow####
Indeces.Add(Values.Count)
Key.Add(KeyIndex, Indeces)
Else 'Key present: Add Index to Container
Key(KeyIndex).Add(Values.Count)
End If
I += 1
Next
Values.Add(Value)
End Sub
'#### OR #####
Public Sub Add(ByVal Value As tValue, ByVal KeyIndeces As iKeyIndeces)
Dim I As Integer
Dim KeyIndex As Object
For Each Key As Dictionary(Of Object, List(Of Integer)) In Keys.Values
KeyIndex = KeyIndeces.GetKey(I, Value)
If Not Key.ContainsKey(KeyIndex) Then
Key.Add(KeyIndex, New Integer() {Values.Count})
Else
ReDim Preserve Key(KeyIndex)(Key(KeyIndex).Length) '####Extremely slow####
Key(KeyIndex)(Key(KeyIndex).Length - 1) = Values.Count
End If
I += 1
Next
Values.Add(Value)
End Sub
Is there a way to speed up the redimensioning of the Integer array?
I already tried the ObjectModel.Collection(Of Integer) Object and it is a little faster than List(Of Integer),
but still while adding it takes almost twice the time it takes with an Integer array.
For comparsion:
Using just an Integer (No array therefore no duplicate Key possible):
Adding 2million Object => 3812.5 ms
Using Integer array (duplicate Keys possible):
Every key unique, 2m objects => 4968.75 ms
Always same key, 2m obj => aborted >2min
Using ObjectModel.Collection(Of Integer) (...):
Every key unique, 2m obj => 8421.875 ms
Always same key, 2m obj => 2125.125 ms
Using List(Of Integer) (...):
Every key unique, 2m obj => 7437.5 ms
Always same key, 2m obj => 2125.125 ms