Question

I initially was looking for a way convert byte to float, and found answers that indicate the fastest was is to create a lookup table.

So i was wondering if anyone know of a pre-existing lookup table that i can use.

Was it helpful?

Solution

Normally you would initialize the lookup table using a few lines of code and a for loop or whatever suits your purpose. It's only useful if you're doing a large number of conversions on a finite number of possible inputs.

The example below is only to demonstrate the basic technique of building and using a lookup table. Unless there is more math involved, there would actually be a performance hit if you implemented this (see below).

float[] lookupTable = new float[256];
for (int i = 0; i < 256; i++)
{
    lookupTable[i] = (float)i;
}

float convertedValue = lookupTable[byteValue];

The code is C#, I have no experience with objective C. In C++ the array declaration would be a bit different, but you get the idea.

When to use a lookup table?

In the above example, there is no performance gain, because no calculation is involved, just a conversion from byte to float. Consider the case where floating point division is involved (like your case):

    lookupTable[i] = i / 255f;

In this case, the lookup table should be faster than using direct calculation. The more complex the math (trigonometry, etc.), the bigger the performance gain. Another common usage is gamma correcting an image (exponential function).

OTHER TIPS

Lookup table? We don't need no stinkin' lookup tables!

float floatVal = (float)byteVal;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top