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));
}
}
Output on Java SE6:
(int) (mFloat + 0.5): n1 = 500410000 -> 0.003ms/1000
Math.round(mFloat) : n2 = 499590000 -> 0.022ms/1000
Output on 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 was a huge performance improvement on Math.round
from SE6 to SE7. I think in SE7 there is no significant difference anymore and you should choose whatever seems more readable to you.
Math.round
, you'll see the code from your first line, except for a single special case. This is why the performance is expected to be the same, especially if HotSpot inlines yourround
call. – Linotype