I am working on a small project but have run into a performance roadblock.
I have a Dictionary<string, string>()
I have a string[]
.
Lets say my Dictionary
has 50,000 entries, and my string[]
has 30,000 entries.
I want to collect the Keys
from my Dictionary
where the value.ToCharArray().OrderBy(x => x)
equals a value.ToCharArray().OrderBy(x => x)
of my string[]
.
I have tried reducing the number of KeyValue
pairs I have to look through by comparing the length of my string[]
value to the values in the Dictionary
, but that has not really gained me any performance.
Does anyone have an ideas how I can improve the performance of this lookup?
Thanks!
To expand the pseudocode:
var stringToLookUp = GetSomeStrings(s.ToString()).Select(x => x).OrderBy(x => x).ToArray();
var aDictionaryOfStringString = GetDictionary(Resources.stringList);
var results = new List<string>();
foreach (var theString in stringToLookUp.Where(aString=> aString.Length > 0))
{
if (theString.Length > 0)
{
var theStringClosure = theString;
var filteredKeyValuePairs = aDictionaryOfStringString.Where(w => w.Value.Length == theStringClosure.Length && !results.Contains(w.Key)).ToArray();
var foundStrings = filteredKeyValuePairs.Where(kv => kv.Value.ToCharArray().OrderBy(c => c).ToArray().SequenceEqual(theStringClosure))
.Select(kv => kv.Key)
.ToArray();
if (foundStrings.Any()) results.AddRange(foundStrings);
}
}