查找 GUID 的快速方法
我有很多(+2000)个 GUID(在某些网络类中),我的程序在收到消息时必须找到其中一个并执行与之相关的工作。
积极的一点是我有一个硬代码生成器,但最快的方法是我的目标(而且我不知道如何实现它)。
我的代码应该做这样的事情:
switch(received guid)
{
case guid1: do job 1; break;
case guid2: do job 2; break;
case guid3: do job 3; break;
case guid4: do job 4; break;
....
}
I have lots(+2000) of GUIDs(in some network class) and my program must find one of them when it receives a message and do the job associated with it.
the positive point is i have a hard-code generator, but the fastest way is my goal(and i don't know how to implement it).
my code should do something like this:
switch(received guid)
{
case guid1: do job 1; break;
case guid2: do job 2; break;
case guid3: do job 3; break;
case guid4: do job 4; break;
....
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以创建一个字典,其中 Guid 作为键,委托引用作为值。这将确保快速查找。
You could create a Dictionary with the Guid as the key and a delegate reference as the value. That would ensure fast lookups.
创建一个用于执行作业的接口,然后实现 2000 个执行该工作的类,每个类都知道自己的 guid。然后使用其 guid 作为键将类添加到字典中。然后,当您获得 guid 时,您可以在字典中查找该对象并调用接口上的方法。
Create an interface for doing the Job, then implement 2000 classes which do the work, each of which knows its own guid. Then add the classes to a dictionary using its guid as the key. Then when you get the guid, you look the object up in the dictionary and call the method on the interface.
使用将 Guid 映射到代表任务的委托或类的哈希表,例如
Dictionary
或Dictionary
。Use a hashtable which maps Guid to a delegate or a class that represents the task, such as
Dictionary<Guid, Action>
orDictionary<Guid, Task>
.Dictionary
可能比 switch 语句更快。但你必须进行分析才能确定。
A
Dictionary<Guid, JobDelegate>
would probably be faster than a switch statement.But you would have to profile to be sure.
我喜欢展示其他人已经提出的字典方法的变体。在此解决方案的基础上,您可以执行以下操作。
1 定义一个基类:
2 定义一个用于装饰工作执行者的属性。
3 定义用该属性修饰的实际作业执行者类。例如:
4 定义一个
JobDoerFactory
,允许通过属性中定义的 Id 检索JobDoer
实例:在
BuildCache
方法中,您可以可以使用反射来加载JobDoer
实例。我没有测试这段代码,所以我不知道它是否可以编译,但几年前我在一个项目中使用了这种机制。这样就可以很容易地定义新类,而不需要将其连接到某些字典。它是在运行时自动完成的。
这可能看起来有点大材小用,但如果您有超过 2000 个
JobDoer
类,这会对您有很大帮助。更新:
请注意,如果您不喜欢
JobDoerAttribute
的想法,您还可以将其实现为抽象JobDoer
类上的抽象属性。但是,我发现使用属性使代码非常明确且富有表现力。I like to show a variation of the dictionary approach others already proposed. Building on this that solution, you could do the following.
1 Define a base class:
2 Define a attribute for decoration of job doers.
3 Define the actual job doer classes that are decorated with that attribute. For instance:
4 Define a
JobDoerFactory
that enables retrievingJobDoer
instances by their Id as it is defined in the attribute:In the
BuildCache
method, you can do the loading ofJobDoer
instances by using reflection.I didn't test this code, so I don't know if it compiles, but I used this mechanism in a project a few years back. This way it is easy to define new classes, without the need to hook it up to some dictionary. It is done automatically at runtime.
This might look a bit like overkill, but if you have +2000
JobDoer
classes, this could help you a lot.Update:
Note that if you don't like the idea of the
JobDoerAttribute
, you can also implement it as abstract property on the abstractJobDoer
class. However, I've found using an attribute makes the code very explicit and expressive.使用 Guid 和 Action 创建一个字典,然后对其进行搜索。
Create a Dictionary with Guid And Action, and search over it.