I know it’s hard to believe, but I have just come across a Math.abs() bottleneck in my game code, specifically when I have lots of physics obejcts bouncing around, the object to object collision check does a first/quick Manhattan (Lm) distance check. This is basically like an Axis-Aligned Bounding Box check but without the box. Anyway, Manhattan distance uses the absolute value difference between points. I’m doing allot to keep from doing more expensive tests and BOOM! profiler shows the test as second highest method call!
I should mention I am using floats, so the little bit hacks don’t help here, but I am trying several tests…
Well at least in the NetBeans profiler…
x = ( x > 0 ) ? x : -x;
is faster than
x = Math.abs(x);
Could be a lie though because the profiling might prevent the VM optimizations for Math.abs()…
[Last mod of the night]
of course…
if ( x < 0 ) { x = -x; }
is faster than
x = ( x > 0 ) ? x : -x;
Because half the time (in general) there will be no asignment done (i.e. x > 0) so i ended with that.