Hello,
As a general comment, the Java microbenching version of "Pics or it
didn't happen" should be "jmh or it isn't faster" ;-)
As Andrew's other comment notes, the performance details can be
processor and platform specific so discerning small performance
differences really needs to be data-driven.
Thanks,
-Joe
On 6/5/2016 11:17 AM, Jeff Hain wrote:
Hi.
While playing around with Math.round(double) code,
I found out that
if (longBits < 0) {
r = -r;
}
can be replaced with:
long bitsSignum = (((longBits >> 63) << 1) + 1); // 2*0+1 = 1, or 2*-1+1 = -1
r *= bitsSignum;
which seems a bit faster, as one could expect due to less branching.
NB: The playing was making an "int roundToInt(double)" method.
I think it would be good to have such a method in Math
(and also versions for short/byte?),
because people often use "(int) Math.round(foo)" for this use case,
which doesn't work when out of int range.
Moreover, people should rarely catch the bug in their tests,
since they usually don't test with pathological values.
Recent example:
http://alvinalexander.com/java/how-to-round-float-double-to-int-integer-in-java
-Jeff