Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most efficient way to do look-up table in C#

What is the most efficient way to do look-up table in C#

I have a look-up table. Sort of like

0 "Thing 1"
1 "Thing 2"
2 "Reserved"
3 "Reserved"
4 "Reserved"
5 "Not a Thing"

So if someone wants "Thing 1" or "Thing 2" they pass in 0 or 1. But they may pass in something else also. I have 256 of these type of things and maybe 200 of them are reserved.

So what is the most efficient want to set this up?

  • A string Array or dictionary variable that gets all of the values. And then take the integer and return the value at that place.

One problem I have with this solution is all of the "Reserved" values. I don't want to create those redundant "reserved" values. Or else I can have an if statement against all of the various places that are "reserved" but they might now be just 2-3, might be 2-3, 40-55 and all different places in the byte. This if statement would get unruly quick

  • My other option that I was thinking was a switch statement. And I would have all of the 50ish known values and would fall through through and default for the reserved values.

I am wondering if this is a lot more processing than creating a string array or dictionary and just returning the appropriate value.

  • Something else? Is there another way to consider?
like image 680
Maestro1024 Avatar asked Dec 06 '09 16:12

Maestro1024


People also ask

How is lookup table faster in execution?

The execution speed is faster than that for unevenly spaced data, because the position search is faster and the interpolation requires a simple division.

What is a lookup table in C?

Well a lookup table is simply an initialized array that contains precalculated information. They are typically used to avoid performing complex (and hence time consuming) calculations. For example, it is well known that the speed of CRC calculations may be significantly increased by use of a lookup table.

What is the advantage of constant array for lookup table?

It's generally a good rule to declare the lookup table as const array : the compiler will optimize the array (for example by putting it in low-speed write time and good access-time memory banks).

What is lookup table with example?

In data analysis applications, such as image processing, a lookup table (LUT) is used to transform the input data into a more desirable output format. For example, a grayscale picture of the planet Saturn will be transformed into a color image to emphasize the differences in its rings.


1 Answers

"Retrieving a value by using its key is very fast, close to O(1), because the Dictionary(TKey, TValue) class is implemented as a hash table."

var things = new Dictionary<int, string>();
things[0]="Thing 1";
things[1]="Thing 2";
things[4711]="Carmen Sandiego";
like image 74
Jonas Elfström Avatar answered Sep 21 '22 16:09

Jonas Elfström