Optimization tricks, lookup table for float values?

hi,

Is it somehow possible to make lookup table for this kind of function,

float square(float n)
{
	return n*n;
}

value n is always between -0.5 to 0.5,
and the script accesses it about 500k times per rendered image, so it might help (?)

The operation of looking the value up is going to cost more then the math itself.

In my experience, any LUT you make is going to take way longer to get a result than a simple n*n. It might make sense for more complicated operation, but for a multiply, I don’t think its going to be worth it. I personally use LUT for an Integer.ToString function. But thats about it.

thanks!

Going to investigate the next function, which is “Mathf.Sqrt”,

its called 25million times…

stats:
min:1.421085E-14 max:25 loops:25607280

Going to read about it 1st, to see how it works…

sqrt and beyond could lead to speed gains on some mobile platforms. I don’t see any point to LUTs usually for the above reasons. Certainly not much for desktop.

I used to see some badass gains for cos/sin/sqrt approx for 3G, but I haven’t tried them since those days.

The idea of lookup tables is old-school, back when CPUs were slow, but these days memory is generally far slower than CPUs, so it’s usually better just to compute stuff instead of accessing RAM. (Really complicated stuff excepted, of course.)

–Eric

I think a much better place to look for optimizations would be why are you calling Square Root 25 million times.
Can you:
A) Store the result of each Square Root operation, such that calling Sqrt on the same value would use the cached value.
B) Perform this as a preprocess, so that it’s done prior to your game running.
C) Find other ways to cut down on the amount of times you do it.

Instead of looking for how you can do these operations faster, try see if you can just avoid doing some of them.

If it is a distance check, consider using the square of the distance, or if you don’t care too much about accuracy, Manhattan distance or even a modified Manhattan distance to reduce the error margin.

But before I did that, I would certainly look in to why you are calling sqrt so many times.

Thanks for the tips…looking into those.

this is what i’m testing (just for fun/learning),
some ray tracing code converted to unity,

For the Mathf.Sqrt()

I tried,
Babylonian Method (fast, but bad quality…), Bakhshali Approximation
http://www.boostworthy.com/blog/?p=197

Also tried,
simple lookuptable (if value is int)

Also this one,
Babylonian Method “inline”,
this might work if could further optimize it…(good quality, even when remove the additional lines)
http://osflash.org/as3_speed_optimizations#math.sqrt

this could be something, but needs “unsafe block”…(?)

moving on to the next parts…