Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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";
     }
}

I am open to any and all ideas!!
Thanks in advance!
-Jeff
Posted
Comments
William Winner 22-Jul-10 17:41pm    
good god...you typed in over 18,000 individual case statements?

It seems like you should really have a database with all of that in it and then you can just run a query on the db. Plus, if you have to add something, you don't have to re-write your code.
Burdzy 23-Jul-10 10:42am    
lol, No I didn't type in 18,000 case statements, I don't hate my life that much!! The data was pulled from an IEEE Standards authority Text File. It's a 1 to 1 mapping of Company ID's to registered Company Names. I downloaded the current text file and scripted the creation of the case return statements in EditPlus (Which is a GREAT PROGRAM by the way!!). The reason for not wanting to attach a database is that I am working on Writing a stand-alone executable that I can send out to several people. I don't want to have to worry about installing a DataBase on everyone's computer. As far as I can tell, the list doesn't get updated tooo frequently, so if there is a major update, I would have to update everyones computer anyway, so i am not too worried about it!!
Yusuf 23-Jul-10 12:47pm    
Your logic does not seem right. Are you sure you want to update your program every time IEEE updates the file? I'd either create database or flat flat as input, then when in the input changes you don't have to change your program.

Seriously, it seems right candidate for DB. Since you said you don't want to install db, then how about flat file, either using some delimiter or XML file.

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.
 
Share this answer
 
Comments
Dalek Dave 22-Jul-10 15:27pm    
Good answer
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.
 
Share this answer
 
Comments
Burdzy 22-Jul-10 13:45pm    
I couldn't use an array as I am using a string as my lookup value. It's a good idea about using a database, but that's really the only information that I have for the company (I am not dealing with address, contacts, etc.) I found another possibility, though I am not sure the implications as I haven't used them much: Since my data really isn't changing much, could I add them all as Resource Strings and then query the resourcemanager using the resourceManger.GetString("Key"); ?? Think that would be quicker/a better approach? I will do some research on the Resource manager, and post any findings. Thanks guys for the quick responses!!
Gregory Gadow 22-Jul-10 13:57pm    
The problem with resource strings is that, you will need to rewrite your application when -- not if -- that data changes. I'm not sure how it would affect performance, as I've never tried that approach.
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
 
Share this answer
 
Comments
Dalek Dave 23-Jul-10 11:06am    
Glad to see you have been 'Solved' :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900