1

What way is the best to get minimum number and which improved the performance or both are same as performance wise?

One way to get the min distance between two number :

double minDistance = Double.MAX_VALUE;
double distance = coordinate1.distnaceTo(coordinate2);

if(distnace  < minDistance ) {
  minDistance = distance;
}

Another way to get the min distance between two number :

double minDistance = Double.MAX_VALUE;
double minDistance = Math.min(coordinate1.distnaceTo(coordinate2), minDistance);
Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
  • 1
    note that the second one will not compile since you have `minDistance` already defined. And you have a typo in `distnaceTo` - should be `distanceTo`. And one more in the first `if`-condition. – luk2302 Oct 16 '15 at 09:49
  • 1
    Use whichever makes your code easier to follow and maintain. The difference between using `if` and using `Math.min` is not going to affect your performance. – khelwood Oct 16 '15 at 09:51
  • should the first case read `if (distance < minDistance) { distance = minDistance; }` ? And the second case `double distance = Math.min...` ? – vikingsteve Oct 16 '15 at 10:38

2 Answers2

5

If you are dealing with positive numbers, not equal to NaN, -0.0 or 0.0, then there shouldn't be much difference.

The following from the Math.min source code in Java 8 highlights the following differences:

If either value is NaN, then the result is NaN. Unlike the numerical comparison operators, this method considers negative zero to be strictly smaller than positive zero. If one argument is positive zero and the other is negative zero, the result is negative zero.

So Math.min may be slightly less efficient since it checks for NaN and -0.0 vs 0.0, but it is arguably more readable - you need to consider firstly whether the special cases are applicable or not, and then readability vs (ever so slight) performance difference after that.

Personally I would use Math.min, but that's my own opinion.

akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
vikingsteve
  • 38,481
  • 23
  • 112
  • 156
3

Java Math#min(double, double) does the following:

  • Returns the smaller of two double values. That is, the result is the value closer to negative infinity.
  • If the arguments have the same value, the result is that same value.
  • If either value is NaN, then the result is NaN.
  • If one argument is positive zero and the other is negative zero, the result is negative zero.

Take a look at the source:

public static double min(double a, double b) {
    if (a != a) return a;   // a is NaN
    if ((a == 0.0d) && (b == 0.0d) && (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) {
        return b;
    }
    return (a <= b) ? a : b;
}

And here is your implementation:

if(distnace  < minDistance ) {
   ....
}

So, yes your code is a little bit faster than Math.min() as it checks some additional condition, NaN, zero or negativity whereas your if-else doesn't care any of those.

Community
  • 1
  • 1
mazhar islam
  • 5,561
  • 3
  • 20
  • 41