Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>I have it running in 0.34 seconds</strong> down from 9+ minutes</p> <p>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.</p> <p>From what I can determine, the KeyValuePair.GetHashCode() returns the hashcode of it's <code>Key</code> object (in this example the least unique object).</p> <p>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.</p> <p>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.</p> <p>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)</p> <p>Hope this helps :)</p> <pre><code> class StringPairComparer : IEqualityComparer&lt;KeyValuePair&lt;string, string&gt;&gt; { public bool Equals(KeyValuePair&lt;string, string&gt; x, KeyValuePair&lt;string, string&gt; y) { return x.Value == y.Value &amp;&amp; x.Key == y.Key; } public int GetHashCode(KeyValuePair&lt;string, string&gt; obj) { return (obj.Key + obj.Value).GetHashCode(); } } </code></pre> <p><strong>EDIT</strong>: 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 <em>faaaaaar too much time on it</em> (I learned quiet a bit though)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload