简体   繁体   English

在C#中,其键的类型类型的字典是否比其键的类型为ulong的字典慢?

[英]In C#, is a dictionary whose keys are of type Type slower than a dictionary whose keys are of type ulong?

I currently have the following: 我目前有以下内容:

ConcurrentDictionary<ulong, MyEventHandler> eventHandlers;

and an indefinite number of classes: 以及无限个类:

class MyClass1
{
    public const ulong MyKey = 0;
    ... 
}

class MyClass2
{
    public const ulong MyKey = 1;
    ... 
}

...etc... ...等等...

My dictionary is to hold an event handler that corresponds to the type of class. 我的字典是保存一个与类类型相对应的事件处理程序。 Right now I'm using the MyKey member as the key to my dictionary, which works fine. 现在,我使用MyKey成员作为字典的键,可以正常工作。

However, when other classes are developed now and in the future, I don't want to make the developer worry about having to have a key. 但是,当现在和将来开发其他类时,我不想让开发人员担心必须拥有密钥。

How much slower (if at all) is doing the following: 执行以下操作的速度要慢多少(如果有的话):

ConcurrentDictionary<Type, MyEventHandler> eventHandlers;

and then using the typeof operator to get the Type to index into my dictionary? 然后使用typeof运算符将Type索引到我的字典中?

That way I never have to worry about a key. 这样,我就不必担心钥匙。

I do care a lot about speed, because even though the dictionary likely wont have more than 100 entries, typeof will be called and the dictionary will be accessed thousands of times per second. 我确实非常在意速度,因为即使字典可能不会有超过100个条目,也会调用typeof并每秒对字典进行数千次访问。

Types are represented and identified by metadata tokens, which I guess are 32-bit integers. 类型由元数据令牌表示和标识,我猜这是32位整数。 The typeof operator makes a lookup in metadata tables to find the Type represented by the metadata token. typeof运算符在元数据表中进行查找,以找到由元数据令牌表示的Type。

I believe that the JIT compiler will optimize metadata table accesses very well and therefore I don't think that you should notice any performance difference in reality. 我相信JIT编译器会很好地优化元数据表访问,因此,我认为您不应该注意到现实中的任何性能差异。 I would actually not be surprised if using Type as key would even prove to be slightly faster. 如果使用Type作为键甚至会证明速度稍快,我实际上不会感到惊讶。

Since using Type as key would make the code much easier to maintain I would recommend that approach unless you can prove that the other approach is clearly more performant. 因为使用Type作为键会使代码更易于维护,所以我建议您采用这种方法,除非您可以证明另一种方法显然更有效。

If your dictionary is a singleton/multiton you can create a generic type to speed up indexation by this pattern : 如果您的字典是单例/多例,则可以通过以下模式创建通用类型以加快索引编制速度:

static class MyHandler<T>
{
    static public MyEventHandler Value;
}

instead of 代替

CurrentDictionary[typeof(Class1)]

you can do 你可以做

MyHandler<Class1>.Value

This depends on how GetHashCode() is implemented. 这取决于如何实现GetHashCode() WHen the .Net framework code "finds" an object in a dictionary, it calls this method to perform the search. 当.Net框架代码“查找”字典中的对象时,它将调用此方法来执行搜索。 Obviously, in any Dictionary that uses a key based on a type where the implementation of GetHashCode() takes substantially longer than simply retrieving the value of a long , or a uLong , that dictionary will exhibit proportionally poorer performance. 显然,在任何使用基于类型的键的字典中,与仅检索longuLong的值相比, GetHashCode()的实现所花费的时间要长得多,该字典的性能将成比例地uLong

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM