^2 speed question
Blitz3D Forums/Blitz3D Programming/^2 speed question
| ||
a*a is about 4 times faster than a^2. How can I write something like: a^1.5 not using the ^ operator, but the * multiplicator only? |
| ||
a^1.5 is equivalent to a*Sqr(a). |
| ||
Mathematically you can't rewrite it except by: a*a^0.5 or a*sqr(a); but I doubt you'd gain speed either way. In the latter you lose the power but you have to rely on the square root function. *edit*Floyd beat me to it |
| ||
Thanks! Uh stupid me, I can easily create a lookup table on the fly since the values are repeating frequently. |
| ||
Do a time test when you've got lookup tables in place and keep both versions of the code handy. Once you've got that part of your project finished, do another time test. The reason being is that the lookup table that fits into your CPU's data cache today, might not fit tomorrow. If it doesn't fit in the cache, it will probably end up being a lot slower than getting the CPU to calculate on the fly. |
| ||
thanks. the table is definitively faster since it has only 256 fields, compared to 256^3 calculations / table accesses. |
| ||
If you feel comfortable, you could step into the weird and wonderful world of logarithms... if x= a^1.5 then log x = 1.5 * log a This could be handy if you have to calculate a lot of different powers of 'a' as you can get the log once and then use this value in a whole series of simple multiplications. You would then use exp(x) to 'translate' them back into normal numbers. To be honest, your ALU/FPU is probably going to handle this realtime better than most hand-coded optimisations (I guess!) |