What is the most efficient way to round a float value to the nearest integer in java?

I have seen a lot of discussion on SO related to rounding float values, but without solid Q & A considering performance aspects. So here it is:

What is the most efficient (but correct) way to round a float to the nearest integer?

(int) (mFloat + 0.5); 

or

 Math.round(mFloat); 

or

 FloatMath.floor(mFloat + 0.5); 

or something else?

Preferably, I would like to use something available in standard java libraries, and not some external library that I have to import.

+2
java optimization floating-accuracy rounding
Aug 23 2018-12-12T00:
source share
5 answers
 public class Main { public static void main(String[] args) throws InterruptedException { for (int i = 0; i < 10; i++) { measurementIteration(); } } public static void measurementIteration() { long s, t1 = 0, t2 = 0; float mFloat = 3.3f; int f, n1 = 0, n2 = 0; for (int i = 0; i < 1E4; i++) { switch ((int) (Math.random() * 2)) { case 0: n1 += 1E4; s = System.currentTimeMillis(); for (int k = 0; k < 1E4; k++) f = (int) (mFloat + 0.5); t1 += System.currentTimeMillis() - s; break; case 1: n2 += 1E4; s = System.currentTimeMillis(); for (int k = 0; k < 1E4; k++) f = Math.round(mFloat); t2 += System.currentTimeMillis() - s; break; } } System.out.println(String.format("(int) (mFloat + 0.5): n1 = %d -> %.3fms/1000", n1, t1 * 1000.0 / n1)); System.out.println(String.format("Math.round(mFloat) : n2 = %d -> %.3fms/1000", n2, t2 * 1000.0 / n2)); } } 

Java SE6 output:

 (int) (mFloat + 0.5): n1 = 500410000 -> 0.003ms/1000 Math.round(mFloat) : n2 = 499590000 -> 0.022ms/1000 

Exit to Java SE7 (thanks to alex for the results):

 (int) (mFloat + 0.5): n1 = 50120000 -> 0,002ms/1000 Math.round(mFloat) : n2 = 49880000 -> 0,002ms/1000 

As you can see, there has been a significant performance improvement at the Math.round level from SE6 to SE7. I think that there are no more significant differences in SE7, and you should choose what seems more readable to you.

+4
Aug 23 '12 at 12:13
source share
— -

Based on the Q & A, which I think of, you mean, the relative effectiveness of the various methods depends on the platform you use.

But the bottom line is this:

  • recent JREs have a performance patch for Math.floor / StrictMath.floor and
  • if you don’t do a lot of rounding, it probably doesn’t matter how you do it.

Literature:

+5
Aug 23 2018-12-12T00:
source share

I have to go for Math.round(mFloat) because it encapsulates the rounding logic in a method (even if it's not your method).

According to the documentation, the code you wrote matches the Math.round (unless it checks for boundary cases).

In any case, the complexity of the time of your algorithm is more important than the time for small things like constants ... In addition, you program something that will be called millions of times !: D

Edit: I don't know FloatMath. Is it from JDK?

0
Aug 23 2018-12-12T00:
source share

You can compare it using System.currentTimeMillis() . You will see that the difference is too small.

0
Aug 23 2018-12-12T00:
source share

Just adding 0.5 will give an incorrect result for negative numbers. See Faster implementation of Math.round? for a better solution.

0
Feb 04 '15 at 19:03
source share



All Articles