what is the fastest way to generate a unique set in .net 2

前端 未结 6 813
离开以前
离开以前 2021-01-12 21:46

I have what is essentially a jagged array of name value pairs - i need to generate a set of unique name values from this. the jagged array is approx 86,000 x 11 values. It d

6条回答
  •  遥遥无期
    2021-01-12 22:07

    I have it running in 0.34 seconds down from 9+ minutes

    The problem is when comparing the KeyValuePair structs. I worked around it by writing a comparer object, and passing an instance of it to the Dictionary.

    From what I can determine, the KeyValuePair.GetHashCode() returns the hashcode of it's Key object (in this example the least unique object).

    As the dictionary adds (and checks existence of) each item, it uses both Equals and GetHashCode functions, but has to rely on the Equals function when the hashcode is less unique.

    By providing a more unique GetHashCode function, it excerises the Equals function far less often. I also optimised the Equals function to compare the more unique Values before the less unqiue Keys.

    86,000 * 11 items with 10,000 unique properties runs in 0.34 seconds using the comparer object below (without the comparer object it takes 9 minutes 22 seconds)

    Hope this helps :)

        class StringPairComparer
            : IEqualityComparer>
        {
            public bool Equals(KeyValuePair x, KeyValuePair y)
            {
                return x.Value == y.Value && x.Key == y.Key;
            }
            public int GetHashCode(KeyValuePair obj)
            {
                return (obj.Key + obj.Value).GetHashCode();
            }
        }
    

    EDIT: If it was just one string (instead of a KeyValuePair, where string = Name+Value) it would be approx twice as fast. It's a nice intresting problem, and I have spent faaaaaar too much time on it (I learned quiet a bit though)

提交回复
热议问题