user1437001
user1437001

Reputation: 53

Performance issue with DictionaryEntry looping

Currently I have 231556 of words collection and do below loop to check every words for duplication.

I am using this function :-

public bool IsContainStringCIAI(string wordIn, HybridDictionary hd, out string wordOut)
    {
        int iValue = 1;
        foreach (DictionaryEntry de2 in hd)
        {
            iValue = CultureInfo.CurrentCulture.CompareInfo.Compare(wordIn.ToLower(), de2.Key.ToString().ToLower(), CompareOptions.IgnoreNonSpace);
            if (iValue == 0)
            {
                wordOut = de2.Key.ToString(); //Assign the existing word
                return true;
            }
        }
        wordOut = wordIn;
        return false;
    }

It take around 20 hours to finish looping, because each word will be added in to dictionary after comparing if it is not same. Anything can I do to improve this loop? Thanks before.

Upvotes: 0

Views: 139

Answers (1)

Andrew Kennan
Andrew Kennan

Reputation: 14157

Can you convert your HybridDictionary to a Dictionary<string, string> where all the keys are already converted into a format you can compare (lower case, unwanted characters stripped out, whatever)? Then your method pretty much becomes this:

return hd.TryGetValue(wordIn.ToLower(), out wordOut); 

And Dictionary is pretty fast ;]

Upvotes: 1

Related Questions