Dictionary<Type,Object> 与 Dictionary<RuntimeTypeHandle,Object> 哪个更能体现性能?

Dictionary<Type,Object> vs Dictionary<RuntimeTypeHandle,Object> what's more performance wise?

什么是更明智的性能?散列 RuntimeTypeHandle 或散列 Type?请尝试解释原因。

对于 "whihc is faster" 的 任何 问题,唯一确定的回答方法是用不同的方法尝试相同的事情并比较结果。这可能意味着要进行一百万次才能获得显着差异,但其他任何事情都是学术性的。

也就是说,字典性能的主要区别在于它们的键类型,特别是它们的哈希算法的性能和分布。

查看 RuntimeTypeHandle.GetHashCode 的源代码,您可以看到它使用底层 RuntimeType 实例的哈希码。

    private RuntimeType m_type;

    public override int GetHashCode()
    {
        return m_type != null ? m_type.GetHashCode() : 0;
    }

现在 RuntimeType 不会覆盖 GetHashCode,所以它很大程度上来自它的继承类型 TypeInfoTypeInfo 没有覆盖GetHashCode,所以它使用继承的类型,即Type

因此,由于 RuntimeTypeHandleGetHashCode 实现与 Type 相同,我预计不会出现 显着的 性能差异。