很大的Switch Case语句,第一次需要很长时间才能运行... [英] Very large Switch Case statement, takes a long to run the first time...

查看:558
本文介绍了很大的Switch Case语句,第一次需要很长时间才能运行...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要基于字符串(C#2.x)对字符串进行查找.我已将其实现为switch case语句,并且第一次运行需要花费2-3秒的时间,但是在那之后它似乎是瞬时的.我假设这是因为它是在第一次运行时建立哈希/跳转表,然后才查找值.我的问题是,有没有办法让我对其进行预编译,使其始终运行缓存"并即时"运行,而不是第一次延迟2-3秒?我也一直在尝试找到一些使用switch语句的替代方法,例如字典或其他内容,但实际上并没有找到更好的解决方案.我在c#3.0中找到了一种使用静态字典的方法,但这不是我的选择.我的Switch语句目前有18,356多个案例.这是一个示例,可以更好地说明我正在使用的工具:

Hi,
I need to do a lookup of a string based on a string (C# 2.x). I have it implemented as a switch case statement and it takes a solid 2-3 seconds to run the first time, but after that it seems to be instantaneous. I am assuming that it is because it is building the hash/jump table the first time it runs, and after that, it is just looking up the values. My question is, is there a way for me to pre-compile it so that it will always run "Cached" and be "instantaneous" instead of delaying for 2-3 seconds the first time?? I have also been trying to find some alternatives to using a switch statement, i.e. a dictionary, or something else, but haven''t really found a better solution. I found a way to do it using a static dictionary in c# 3.0 but that isn''t an option for me. My Switch statment currently has 18,356+ cases. Here is a sample to better illustrate what I am working with:

private string CompanyFromID(string strCompany_ID)
{
     switch (strCompany_ID)
     {
          case "FCD4F6":
          return "Messana Air.Ray Conditioning s.r.l.";
          case "FCCCE4":
          return "Ascon Ltd.";
...
          default:
          return "Unknown";
     }
}


我愿意接受任何想法!
提前谢谢!
-Jeff


I am open to any and all ideas!!
Thanks in advance!
-Jeff

推荐答案

我认为您不能从这里到达那里.据我了解,代码实际上是编译为通用中间语言(CIL)的.在实际运行代码之前,不会将其转换并链接为可执行文件.因为switch语句必须被编译为一个单元,所以整个块-语句的所有36,000行-都在第一次运行时进行编译.

您可能会发现将语句转换为数据存储(例如SQL或XML)更加容易.每次通过代码都会找到匹配项,因此会花费更长的时间,但是实现所需的行数更少,从而避免了编译滞后.它也将具有更大的可扩展性,因为您可以添加或删除公司而不必重写代码.您还可以将有关公司的更多信息(例如地址,联系方式等)组合在一起.
I don''t think you can get there from here. As I understand it, code is actually compiled into a Common Intermediate Language (CIL); it is not converted and linked into an executable until the code is actually run. Because a switch statement must be compiled as a single unit, the entire block -- all 36,000 or so lines of it -- gets compiled the first time it is run.

You may find it easier to convert your statement into a datastore, maybe SQL or XML. Each pass through the code will take a bit longer as it finds a match, but it would require fewer lines to implement and thus avoid the compile lag. It would also be much more scalable, as you can add or remove companies without having to rewrite your code. You could also group together more information about the companies, such as addresses, contacts, etc.


最好形成一个数组并在代码的开头运行它.

然后对数组进行比较.

但是您对哈希表的看法是正确的.
It may be better to form an array and have that run at the start of the code.

Then run a compare to the array.

But you are right about the hashtable.


感谢大家的回应和反馈!因此,我选择了资源"路线,并取得了不错的成绩.表现很棒!运行与缓存开关的情况一样快,但是第一次运行就这样(对我来说很重要,因为它们可能只进行一次查找而不是进行多次查找的情况更多.)一旦添加了它们,我正在使用ResourceManger.GetString(string)方法提取信息:
Thanks everyone for the responses and feed back! So I went with the Resources route and I had great results. The performance is great! Run''s just as fast as the cached switch case, but runs that way the first time (Which was important to me becuase their are probably more occasions of doing only 1 lookup than there are of doing multiple lookups.) Once I added them, I am pulling the info using the ResourceManger.GetString(string) method:
ResourceManager resman = new ResourceManager("<NameSpace>.Properties.Resources", typeof(Resources).Assembly);
string strReturn = resman.GetString(strCompany_ID);
return (String.IsNullOrEmpty(strReturn) ? "Unknown" : strReturn);



如果有人有其他想法,疑虑,提示或意见,我很乐意听到!!

谢谢!

-杰夫



If anyone has any other ideas, Concerns, tips or comments, I''d love to hear them!!

Thanks!

-Jeff


这篇关于很大的Switch Case语句,第一次需要很长时间才能运行...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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