Click here to Skip to main content
15,884,353 members
Home / Discussions / C#
   

C#

 
GeneralRe: Dictionary<K,V> performance based on Key Type Pin
Matty2221-Aug-14 19:11
Matty2221-Aug-14 19:11 
AnswerRe: Dictionary<K,V> performance based on Key Type Pin
Matty2221-Aug-14 19:18
Matty2221-Aug-14 19:18 
GeneralRe: Dictionary<K,V> performance based on Key Type Pin
SledgeHammer0121-Aug-14 19:53
SledgeHammer0121-Aug-14 19:53 
GeneralRe: Dictionary<K,V> performance based on Key Type Pin
Matty2221-Aug-14 19:58
Matty2221-Aug-14 19:58 
GeneralRe: Dictionary<K,V> performance based on Key Type Pin
Matty2221-Aug-14 20:01
Matty2221-Aug-14 20:01 
GeneralRe: Dictionary<K,V> performance based on Key Type Pin
SledgeHammer0121-Aug-14 20:17
SledgeHammer0121-Aug-14 20:17 
GeneralRe: Dictionary<K,V> performance based on Key Type Pin
Matty2221-Aug-14 20:45
Matty2221-Aug-14 20:45 
SuggestionRe: Dictionary<K,V> performance based on Key Type Pin
Richard Deeming22-Aug-14 2:25
mveRichard Deeming22-Aug-14 2:25 
Don't use DateTime for measuring performance; use the Stopwatch class[^] instead.

As you suspect, your "breakthrough" won't work. If you use the result of GetHashCode as a key, you can incorrectly consider two different keys to be the same due to hash code collisions.

You could try making an immutable key class and caching the hash code:
C#
sealed class _Key : IEquatable<Key>
{
    private readonly string _s;
    private readonly Type _t;
    private readonly int _hashCode;

    public _Key(string s, Type t)
    {
        if (s == null) throw new ArgumentNullException("s");
        if (t == null) throw new ArgumentNullException("t");

        _s = s;
        _t = t;

        unchecked
        {
            _hashCode = (s.GetHashCode() * 397) ^ t.GetHashCode();
        }
    }

    public string S
    {
        get { return _s; }
    }

    public Type T
    {
        get { return _t; }
    }

    public override int GetHashCode()
    {
        return _hashCode;
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as _Key);
    }

    public bool Equals(_Key other)
    {
        if (ReferenceEquals(other, null)) return false;
        return _s == other._s && _t == other._t;
    }

    public static bool operator ==(_Key left, _Key right)
    {
        return Equals(left, right);
    }

    public static bool operator !=(_Key left, _Key right)
    {
        return !Equals(left, right);
    }
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


AnswerRe: Dictionary<K,V> performance based on Key Type Pin
Rob Philpott22-Aug-14 0:10
Rob Philpott22-Aug-14 0:10 
QuestionNeed help on using sync framework for devices Pin
krishnapnv21-Aug-14 17:05
krishnapnv21-Aug-14 17:05 
AnswerRe: Need help on using sync framework for devices Pin
Eddy Vluggen22-Aug-14 5:12
professionalEddy Vluggen22-Aug-14 5:12 
QuestionDecoding method Pin
jojoba2021-Aug-14 4:36
jojoba2021-Aug-14 4:36 
GeneralRe: Decoding method Pin
Richard MacCutchan21-Aug-14 4:46
mveRichard MacCutchan21-Aug-14 4:46 
GeneralRe: Decoding method Pin
harold aptroot21-Aug-14 4:46
harold aptroot21-Aug-14 4:46 
GeneralRe: Decoding method Pin
PIEBALDconsult21-Aug-14 4:55
mvePIEBALDconsult21-Aug-14 4:55 
GeneralRe: Decoding method Pin
Bernhard Hiller21-Aug-14 21:43
Bernhard Hiller21-Aug-14 21:43 
GeneralRe: Decoding method Pin
PIEBALDconsult22-Aug-14 3:20
mvePIEBALDconsult22-Aug-14 3:20 
QuestionRe: Decoding method Pin
jojoba2021-Aug-14 6:42
jojoba2021-Aug-14 6:42 
AnswerRe: Decoding method Pin
OriginalGriff21-Aug-14 8:04
mveOriginalGriff21-Aug-14 8:04 
QuestionRe: Decoding method Pin
jojoba2021-Aug-14 9:45
jojoba2021-Aug-14 9:45 
AnswerRe: Decoding method Pin
Eddy Vluggen21-Aug-14 9:53
professionalEddy Vluggen21-Aug-14 9:53 
AnswerRe: Decoding method Pin
Bernhard Hiller21-Aug-14 21:46
Bernhard Hiller21-Aug-14 21:46 
Questionsplit string into line C# Pin
Member 1054819721-Aug-14 3:05
Member 1054819721-Aug-14 3:05 
AnswerRe: split string into line C# Pin
OriginalGriff21-Aug-14 4:07
mveOriginalGriff21-Aug-14 4:07 
GeneralRe: split string into line C# Pin
PIEBALDconsult21-Aug-14 5:32
mvePIEBALDconsult21-Aug-14 5:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.