MathUtils.sqrt() is slower than Math.sqrt() on my computer

The FAST_MATH version of sqrt() takes about 5/3 times as long as Java's sqrt(). That seems silly! Any comments on this?
really? What jvm are you using? I thought I had run tests previously
1.6.0_20-b02 it seems.
At first I thought that the cast to (float) would do the trick, but no, java.sqrt is still getting faster results, even if I remove some accuracy and logic:
Code:
jmath time: 0.009924139
math util time: 0.003103294
jmath time: 0.002805435
math util time: 0.002770146
jmath time: 0.002437597
math util time: 0.003797101
jmath time: 0.002391842
math util time: 0.002590414
jmath time: 0.002386459
math util time: 0.002485446
jmath time: 0.001231807
math util time: 0.003005802
jmath time: 0.002670262
math util time: 0.002609853
jmath time: 0.002299134
math util time: 0.002524024
jmath time: 0.002273416
math util time: 0.002642749
jmath time: 0.002239324
math util time: 0.002690298
jmath time: 0.001200705
math util time: 0.003838371
jmath time: 0.002439391
math util time: 0.002957055
jmath time: 0.002441185
math util time: 0.002839826
jmath time: 0.002281191
math util time: 0.002760576
jmath time: 0.00266428
math util time: 0.002740241


The only question is if this is true for other platforms. I commited the (rather crude) test if others want to try it out on other machines/jvms.

At least the sin/cos lookup table is still faster, although java math did catch up to the lerped version:
Code:
Speed results, in iterations per second
   Table precision   Not lerped       Lerped   Difference    Java Math
        9.99999E-6   5.345909E7   1.629723E7   3.716186E7   2.029414E7
        2.09799E-4   5.206082E7   1.467638E7   3.738444E7   2.062969E7
        4.09599E-4   5.446035E7   1.608478E7   3.837556E7   2.077323E7
        6.09400E-4   5.304358E7   1.584695E7   3.719663E7   2.069608E7
        8.09199E-4   5.462048E7   1.609098E7   3.852950E7   2.052207E7
        0.00100899   5.454924E7   1.617739E7   3.837185E7   2.037700E7
        0.00120880   5.357902E7   1.617896E7   3.740006E7   2.066666E7
        0.00140860   5.530707E7   1.594975E7   3.935731E7   2.035840E7
        0.00160839   5.462943E7   1.558403E7   3.904539E7   2.026585E7
        0.00180820   5.415170E7   1.588384E7   3.826785E7   2.067817E7
        0.00200800   5.357044E7   1.627819E7   3.729224E7   2.014256E7
        0.00220780   5.497973E7   1.562043E7   3.935929E7   2.053467E7
        0.00240760   5.506122E7   1.617269E7   3.888853E7   2.054224E7
        0.00260740   5.513381E7   1.604696E7   3.908684E7   2.055992E7
        0.00280720   5.369087E7   1.616644E7   3.752443E7   2.063988E7
        0.00300700   5.471881E7   1.592469E7   3.879412E7   2.068584E7
        0.00320679   5.255998E7   1.612046E7   3.643952E7   1.815444E7
        0.00340660   5.357902E7   1.576923E7   3.780978E7   2.047683E7
        0.00360640   5.310254E7   1.604619E7   3.705634E7   2.047558E7
        0.00380620   5.282572E7   1.606006E7   3.676565E7   2.066794E7
        0.00400600   5.489848E7   1.611191E7   3.878656E7   2.000997E7
        0.00420580   5.494357E7   1.623788E7   3.870569E7   2.045678E7
        0.00440560   5.451364E7   1.618992E7   3.832372E7   2.065773E7
        0.00460540   5.497970E7   1.628929E7   3.869040E7   1.945352E7
        0.00480520   5.205272E7   1.610337E7   3.594934E7   2.027691E7
        0.00500500   5.439835E7   1.593303E7   3.846531E7   2.053972E7
        0.00520480   5.415167E7   1.629723E7   3.785443E7   2.019243E7
        0.00540460   5.472776E7   1.633305E7   3.839470E7   2.074230E7
        0.00560440   5.401174E7   1.615785E7   3.785389E7   2.036087E7
        0.00580420   5.538954E7   1.628374E7   3.910579E7   2.052082E7
        0.00600400   5.470091E7   1.622055E7   3.848035E7   2.046054E7
        0.00620380   5.447815E7   1.613913E7   3.833902E7   1.947505E7
        0.00640359   5.347622E7   1.631313E7   3.716308E7   2.044678E7
        0.00660340   5.409911E7   1.619463E7   3.790448E7   2.069096E7
        0.00680320   5.415167E7   1.625682E7   3.789484E7   2.056751E7
        0.00700300   5.473672E7   1.616409E7   3.857262E7   2.007122E7
        0.00720280   5.354471E7   1.626473E7   3.727997E7   2.042555E7
        0.00740260   5.479052E7   1.569227E7   3.909825E7   2.056245E7
        0.00760240   5.482648E7   1.623394E7   3.859254E7   2.057131E7
        0.00780220   5.483547E7   1.638427E7   3.845119E7   2.015713E7
        0.00800199   5.470091E7   1.608246E7   3.861844E7   2.046305E7
        0.00820179   5.095803E7   1.631473E7   3.464330E7   2.072045E7
        0.00840159   5.431880E7   1.624656E7   3.807224E7   1.980259E7
        0.00860140   5.314476E7   1.595508E7   3.718967E7   2.049565E7
        0.00880119   5.441608E7   1.633864E7   3.807743E7   2.044053E7
        0.00900099   5.407287E7   1.616566E7   3.790721E7   2.040436E7
        0.00920080   5.506119E7   1.632906E7   3.873212E7   2.050445E7
        0.00940059   5.477258E7   1.624104E7   3.853154E7   2.067816E7
        0.00960039   5.501591E7   1.584095E7   3.917495E7   2.059665E7
        0.00980020   5.426595E7   1.619149E7   3.807446E7   2.022663E7
I ran some more tests.

On my computer, the sine lookup algorithm is faster than Math.sin(), at all levels of precision used by SinCosTest. For some reasonable values, it seems to be twice as fast.

The speed of the lookup algorithm seems to be independent of argument magnitude, for positive arguments. (For huge negative arguments, the += loop in MathUtils might cause trouble.)

The speed of Math.sin() has a strong dependence on argument magnitude. It likes small arguments, especially arguments that are less than 1. But SinCosTest isn't really tricked by this, because it goes up to 2*pi, and many applications of the algorithm wouldn't involve arguments much greater than that. The slowdown due to argument magnitude seems to weaken with distance from zero. Still, for huge arguments, MathUtils.sin() can be 20 times as fast as Math.sin(), on my computer.
I've been runing tests on the "faster" math utils methods.

I setup the following test block:

// timings
AbsoluteTimer timer = new AbsoluteTimer();
timer.start();

int numberIterations = 1000000;
for (int i=0; i<numberIterations; i++)
{
double sinxLookup = lookupTable.sin(22);
}

timer.stop();
double elapsedTime = timer.elapsedTime();
System.out.println("elapsed time: " + elapsedTime);

with output:

elapsed time: 0.097

If I now comment out the mod op call and while loop [if Math.sin() range assumed I find these operations redundant] I get:

public static final double sin(double x)
{
// x %= TWOPI;
// while(x < 0)
// {
// x += TWOPI;
// }
return sinLUT[ round(x / SINCOS_LUT_PRECISION) % SINCOS_LUT_LENGTH];
}

elapsed time: 0.0090

showing an order of magnitude faster.

However, I now run Math.sin() the time is effectively 0!!

I'm using Netbeans and nagitating to the source of Math.sin() we see:

public static double sin(double a) {
return StrictMath.sin(a); // default impl. delegates to StrictMath
}

illustrating that Math.sin() passes the work to StrictMath.sin(), which in turn passes it onto a native call, which is always going to be faster!!

Thus, I have reservations as to whether these "faster" implementations are actually faster. Indeed all tests on my laptop have revealed them to be substantially slower.
First of all, these kind of tests can be very hard to do, as the jvm will start optimizing out your test code. Another issue you don't address is that you have to cast the double returned from StrictMath to a float every time. Native calls are not always going to be faster, that's a common misconception. Take a look at the tests in the google svn, you can mess with them and run them however you want. Here is one of the speed results from running the test with the lookup table vs StrictMath
Code:
Doing speed tests
Speed results, in iterations per second (higher number means faster)
   Table precision   Not lerped       Lerped   Difference    Java Math
        9.99999E-6   4.948758E7   1.529817E7   3.418940E7   1.474112E7
        2.09799E-4   5.251061E7   1.602163E7   3.648897E7   1.478282E7
        4.09599E-4   5.252710E7   1.5773  E7   3.675410E7   1.468931E7
        6.09400E-4   5.387266E7   1.591563E7   3.795702E7   1.477629E7
        8.09199E-4   5.337392E7   1.602701E7   3.734691E7   1.479263E7
        0.00100899   5.181892E7   1.542449E7   3.639442E7   1.474697E7
        0.00120880   5.380330E7   1.611739E7   3.768590E7   1.482346E7
        0.00140860   5.294299E7   1.595056E7   3.699243E7   1.453290E7
        0.00160839   5.188325E7   1.571518E7   3.616807E7   1.405580E7
        0.00180820   5.238720E7   1.608870E7   3.629850E7   1.484517E7
        0.00200800   4.333129E7   1.591867E7   2.741262E7   1.352974E7
        0.00220780   4.822433E7   1.596427E7   3.226006E7   1.476716E7
        0.00240760   4.728999E7   1.601319E7   3.127680E7   1.482214E7
        0.00260740   5.397702E7   1.582675E7   3.815027E7   1.470611E7
        0.00280720   5.409051E7   1.591109E7   3.817942E7   1.432128E7
        0.00300700   5.19155 E7   1.601243E7   3.590307E7   1.422804E7
        0.00320679   5.193966E7   1.577449E7   3.616517E7   1.492401E7
        0.00340660   5.334838E7   1.583274E7   3.751563E7   1.450894E7
        0.00360640   5.330584E7   1.530588E7   3.799996E7   1.462443E7
        0.00380620   5.331433E7   1.576259E7   3.755174E7   1.474567E7
        0.00400600   5.340804E7   1.602931E7   3.737873E7   1.469642E7
        0.00420580   5.337392E7   1.594751E7   3.742640E7   1.460462E7
        0.00440560   5.377740E7   1.591033E7   3.786706E7   1.476716E7
        0.00460540   5.379467E7   1.601013E7   3.778454E7   1.388941E7
        0.00480520   5.353625E7   1.604777E7   3.748847E7   1.415097E7
        0.00500500   5.216658E7   1.585075E7   3.631582E7   1.457915E7
        0.00520480   5.242005E7   1.594599E7   3.647405E7   1.444626E7
        0.00540460   5.301009E7   1.576705E7   3.724304E7   1.443316E7
        0.00560440   5.412552E7   1.598181E7   3.814371E7   1.467255E7
        0.00580420   5.225621E7   1.572553E7   3.653068E7   1.441885E7
        0.00600400   5.333987E7   1.603777E7   3.730210E7   1.397123E7
        0.00620380   5.330587E7   1.601703E7   3.728883E7   1.401104E7
        0.00640359   5.333134E7   1.578044E7   3.75509 E7   1.470934E7
        0.00660340   5.198815E7   1.609334E7   3.589481E7   1.442196E7
        0.00680320   5.334835E7   1.558771E7   3.776064E7   1.448004E7
        0.00700300   5.341654E7   1.594447E7   3.747207E7   1.477890E7
        0.00720280   5.323795E7   1.607091E7   3.716704E7   1.452217E7
        0.00740260   5.235440E7   1.576036E7   3.659404E7   1.453416E7
        0.00760240   5.269264E7   1.560371E7   3.708893E7   1.452280E7
        0.00780220   5.360496E7   1.586354E7   3.774141E7   1.446876E7
        0.00800199   5.364792E7   1.575813E7   3.788978E7   1.465648E7
        0.00820179   5.361352E7   1.61073 E7   3.750622E7   1.431882E7
        0.00840159   5.142842E7   1.585000E7   3.557841E7   1.460909E7
        0.00860140   5.193968E7   1.585075E7   3.608892E7   1.363622E7
        0.00880119   3.228616E7   1.596350E7   1.632265E7   1.451460E7
        0.00900099   5.382930E7   1.572700E7   3.810230E7   1.483727E7
        0.00920080   5.272590E7   1.604162E7   3.668428E7   1.451965E7
        0.00940059   5.180286E7   1.562193E7   3.618093E7   1.250514E7
        0.00960039   5.387266E7   1.553124E7   3.834142E7   1.435201E7
        0.00980020   5.381195E7   1.577969E7   3.803226E7   1.464043E7


Edit: also you have to take other, slower devices onto mind. Their strict math implementation is going to be much slower.
Hi

I was looking through testbed.mathtests.SinCosTest and was wondering what makes it valid with reference to your comment about such tests being difficult?

I can't see what differentiates the tested test from my own.

Do you some how switch off jvm optimisation?

Also, do you have a link for the google svn quoted?
If you just call StrictMath.sin(), then it will eventually be optimized out. Storing the actual value seems to work, you'll notice the "float k;", and then "k = (float)StrictMath.sin()". That also makes sure that there's the cast in there.

here's the one on google:
http://code.google.com/p/jbox2d/source/ ... sTest.java

Edit: what makes these difficult is that you don't know what the jvm will optimize out. When I had just the call there, it was optimized out and I was getting 0 time per loop, which can't be correct. So i tried storing the variable, which seems to work
The way to be 100% sure the JVM won't optimize your code away (especially since in later versions the dead code stripper might get "smarter") is to actually make the output of the program depend on it, apart from timing.

e.g.

Code:
for (int i=0; i<RUNS; ++i) {
  float k = timeThisMethod(...);
}


could theoretically get stripped, especially if the JVM notices that timeThisMethod has no side effects. But...

Code:
float k = 0f;
for (int i=0; i<RUNS; ++i) {
  k += timeThisMethod(...);
}
System.out.println(k);


can't be optimized away even in theory, as long as the value you're adding to k depends on the work that the method is doing.

Obviously the extra addition and assignment can add to the time taken, so it's best to make sure that you test both versions of the method the same way, then you'll at least get a relative answer as to which is faster.

Another issue: you really want to warm up the JVM first, so that the method you're timing gets compiled before timing it, so you really want something like this:

Code:
final int WARMUP_RUNS = 100000; //or some other large number
float m = 0f;
for (int i=0; i<WARMUP_RUNS; ++i) {
  m += timeThisMethod(...);
}
System.out.println(m);

float k = 0f;
startTimer();
for (int i=0; i<RUNS; ++i) {
  k += timeThisMethod(...);
}
stopTimer();
System.out.println(k);


And it's always best to compare two versions of the method with two different programs; JVM optimizations can be stateful, so if you compare both from within the same main method, you might find different results depending on which one runs first.

It's a damn shame Sun never gave us any good ways to do microbenchmarks; it's possible that was deliberate, because microbenchmarks can often be misleading, but still, in cases like this it's annoying to have to jump through so many hoops to figure out which method is faster.

The real proof is to test it in the actual application, though - try delegating the MathUtils calls to Math, and see if the engine runs faster or slower...

Original thread: https://box2d.org/forum/viewtopic.php?t=5286