Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does this mean that Java Math.floor is extremely slow?

I don't Java much.

I am writing some optimized math code and I was shocked by my profiler results. My code collects values, interleaves the data and then chooses the values based on that. Java runs slower than my C++ and MATLAB implementations.

I am using javac 1.7.0_05 I am using the Sun/Oracle JDK 1.7.05

There exists a floor function that performs a relevant task in the code. java math.floor profile results

  1. Does anybody know of the paradigmatic way to fix this?
  2. I noticed that my floor() function is defined with something called StrictMath. Is there something like -ffast-math for Java? I am expecting there must be a way to change the floor function to something more computationally reasonable without writing my own.

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

Edit

So a few people suggested I try to do a cast. I tried this and there was absolutely no change in walltime.

private static int flur(float dF)
{
    return (int) dF;
}

413742 cast floor function

394675 Math.floor

These test were ran without the profiler. An effort was made to use a profiler but the runtime was drastically altered (15+ minutes so I quit).

like image 757
Mikhail Avatar asked Aug 21 '12 06:08

Mikhail


People also ask

Is math floor slow?

It suggests that Math. floor is the SLOWEST way to calculate floor in Javascript.

What is math floor 3.6 in Java?

The Math. floor() function in JavaScript is used to round off the number passed as parameter to its nearest integer in Downward direction of rounding i.g towards the lesser value. Hence, math. floor(3.6) = 3.

Why does math floor return a double Java?

It's for precision. The double data-type has a 53 bit mantissa. Among other things that means that a double can represent all whole up to 2^53 without precision loss. If you store such a large number in an integer you will get an overflow.

What will Math floor do?

The Math. floor() function returns the largest integer less than or equal to a given number.


1 Answers

You might want to give a try to FastMath.

Here is a post about the performance of Math in Java vs. Javascript. There are a few good hints about why the default math lib is slow. They are discussing other operations than floor, but I guess their findings can be generalized. I found it interesting.

EDIT

According to this bug entry, floor has been implemented a pure java code in 7(b79), 6u21(b01) resulting in better performance. The code of floor in the JDK 6 is still a bit longer than the one in FastMath, but might not be responsible for such a perf. degradation. What JDK are you using? Could you try with a more recent version?

like image 117
ewernli Avatar answered Sep 19 '22 16:09

ewernli