In my Java program I have to frequently compute a^b, where a ≥ 0 and 0 ≤ b < 1. I am not satisfied with the speed of Math.pow(a, b). Is there any algorithm that could be potentially faster than Math.pow (at some cost of accuracy)?
Do you need the precise result, or just an approximation?
If a precise result: That's tough.
If you only want an approximation: Try the Nth root algorithm for a fix point iteration as described on wikipedia.
You can then waste use as many clock-cycles for your nth-root approximation as you see fit for your application.
But I guess the Math.pow is already doing that for you. If you want to do it faster, you have to invest some severe work. Even in C (which I know way better than Java) I think writing a really freakin' fast variant of that fix point iteration is not the easiest task (if I was doing nth root computations all the time, and they were the absolute bottle neck, I would resort to multithreading and, if possible, doing a lot of work with SIMD/AVX instructions - or even OpenCL/CUDA).
I believe its hard to find better optimized libraries for math than the standard math library in Java.
Maybe you should look at other optimizations that can be done in your algorithm, like caching frequent values so they can be re-used instead of calculated again and therefore reducing the number of calculations made.
If its mission critical that finding the the n-th root very fast maybe you should consider another language that might has a faster execution time than Java or can perform the power operation more efficiently.
This might be interesting even though the experiment conducted was done in C
What is more efficient? Using pow to square or just multiply it with itself?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With