对于字典主要性能 [英] Key performance for a dictionary

查看:123
本文介绍了对于字典主要性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是一个字符串键比 INT 键在词典&LT快;,&GT ;

推荐答案

没有。首先,词典 [增订]使用密钥哈希code找到它们在其内部存储 - 而不是键。和哈希code是 INT 。对于 INT ,它是 INT 字符串它必须产生

No. First of all, Dictionary [UPDATED] uses hash code of the keys to find them in its internal storage - rather than the keys. And Hashcode is an int. For int, it is just the value of the int, for string it has to be generated.

因此,使用 INT 的速度更快。

So using int is slightly faster.

在事实上产生散列code的字符串是pretty的复杂过程(使用反射段)的希望这不是作为版权突破口,因为它不是的]:

In fact generating hash code for a string is a pretty complex process (snippet using Reflector) [Hope this is not taken as copyright breach because it is NOT]:

fixed (char* str = ((char*) this))
{
    char* chPtr = str;
    int num = 0x15051505;
    int num2 = num;
    int* numPtr = (int*) chPtr;
    for (int i = this.Length; i > 0; i -= 4)
    {
        num = (((num << 5) + num) + (num >> 0x1b)) ^ numPtr[0];
        if (i <= 2)
        {
            break;
        }
        num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr[1];
        numPtr += 2;
    }
    return (num + (num2 * 0x5d588b65));
}

这篇关于对于字典主要性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆