HW Accelerated Square Root
Hi,
I found i need to use square root in a program I am writing.
As it would cause heavy usage and I understood that Math.sqrt is rather heavy duty I looked around for optimized, albeit less accurate, methods but found in the end that Math.sqrt is faster than everything.
I then saw that it is delegated to StrictMath.sqrt with the following comment:
// default impl. delegates to StrictMath
// Note that hardware sqrt instructions
// frequently can be directly used by JITs
// and should be much faster than doing
// Math.sqrt in software.
Does this mean that on my machine it is done in hardware?
Is there a way to know in code if this is so so that I can use the faster alternatives on other machines, where needed?
Thank you
Re: HW Accelerated Square Root
Quote:
Does this mean that on my machine it is done in hardware?
Who knows? I would say that this comment in java.lang.StrictMath would be authoritative:
Quote:
To help ensure portability of Java programs, the definitions of some of the numeric functions in this package require that they produce the same results as certain published algorithms.
I would understand that to mean that if your hardware sqrt result doesn't agree with "certain published algorithms", then it won't be used, but if there are partial results that are usable, then some hw delegation may still occur.
If processing on hardware is important to you, don't use Java. If you want 'Write Once Run Anywhere', do use Java - but it means you have to let the JVM implementers make all your hw optimisation choices for you.
Quote:
found in the end that Math.sqrt is faster than everything
They're probably quite good at what they do.
Quote:
Is there a way to know in code
Unlikely from Java code, I would have thought. Some performance-critical software (jvisualvm is an example that springs to mind) performs metrics when they start up. Could you measure sqrt performance of your available options and make some decisions that way?
Re: HW Accelerated Square Root
Quote:
Originally Posted by
Sean4u
They're probably quite good at what they do.
Actually this whole concern started when i found several sites claiming Math.sqrt is quite slow.
They offered some code that does good enough approximations and that's supposed to be faster.
But when I tried it myself i found the Java version to be faster. So either it really does use HW acceleration or the site was old or just plain wrong. Who knows...
Quote:
Originally Posted by
Sean4u
Could you measure sqrt performance of your available options and make some decisions that way?
I guess I could if this ever becomes a real issue.
I was asking more out of curiosity than anything else.
It doesn't seem like a real problem anyway since it really is very fast.
Thanks