patterncsharpMinor
Is there a better, more efficient way than Dictionary(Of Tuple(), MyClass)?
Viewed 0 times
myclassthanmorewayefficientbettertupledictionarythere
Problem
Suppose I have data coming from different sources that tells me information about a large group of stores that has the following (just an example):
Necessary to define what store we're dealing with:
Store characteristics:
And the structure of the program I'm working with is such that we will always get the first 2 (defining) characteristics and one or more of the store-specific characteristics. My goal is to conglomerate all the characteristics for each of the specific stores together.
To do this, I have created the following class:
And then, in my program, I have created the following:
The key being the
So, now, in my code, supposing I just got some data together with the defining characteristics for a set of stores (might be some, might be all... we don't know if we've seen these before or not even), my loop to put in that data looks as follows:
But this seems to be very slow and I'm wonderin
Necessary to define what store we're dealing with:
- Store_Name (String)
- Location (String)
Store characteristics:
- Square_Feet (Double)
- No_Employees (Double)
- Years_Open (Double)
- ... (Whatever else)
And the structure of the program I'm working with is such that we will always get the first 2 (defining) characteristics and one or more of the store-specific characteristics. My goal is to conglomerate all the characteristics for each of the specific stores together.
To do this, I have created the following class:
Public Class StoreChars
Public Property Square_Feet as Double = 0
Public Property No_Employees as Double = 0
Public Property Years_Open as Double = 0
...
End Class
And then, in my program, I have created the following:
Dim StoreData As New Dictionary(Of Tuple(Of String, String), StoreChars)
The key being the
Store_Name and Location as the defining tuple.So, now, in my code, supposing I just got some data together with the defining characteristics for a set of stores (might be some, might be all... we don't know if we've seen these before or not even), my loop to put in that data looks as follows:
For Each PieceOfInfo in ReturnedData
Dim TupleKey As New Tuple(Of String, String)(PieceOfInfo.Store_Name,
PieceOfInfo.Location)
If Not StoreData.ContainsKey(TupleKey) Then
StoreData.Add(TupleKey, New StoreChars With
{
.Square_Feet = PieceOfInfo.Square_Feet
... (whatever else we got) ...
})
Else
StoreData(TupleKey).Square_Feet = PieceOfInfo.Square_Feet
... (whatever else we got) ...
End If
Next
But this seems to be very slow and I'm wonderin
Solution
If you're stuck with
If you can change the
The problem with the current code is that comparison between keys of type
Tuple as a key, you want to provide an IEqualityComparer> implementation in the constructor of your dictionary.If you can change the
TKey type, you might want to provide a custom class that implements IEquatable.The problem with the current code is that comparison between keys of type
Tuple is not efficient.Context
StackExchange Code Review Q#37679, answer score: 5
Revisions (0)
No revisions yet.