Most efficient way to find smallest of 3 numbers Java?

后端 未结 17 2231
故里飘歌
故里飘歌 2020-12-14 05:44

I have an algorithm written in Java that I would like to make more efficient. A part that I think could be made more efficient is finding the smallest of 3 numbers. Currentl

相关标签:
17条回答
  • 2020-12-14 06:03

    If you will call min() around 1kk times with different a, b, c, then use my method:

    Here only two comparisons. There is no way to calc faster :P

    public static double min(double a, double b, double c) {
        if (a > b) {     //if true, min = b
            if (b > c) { //if true, min = c
                return c;
            } else {     //else min = b
                return b; 
            }
        }          //else min = a
        if (a > c) {  // if true, min=c
            return c;
        } else {
            return a;
        }
    }
    
    0 讨论(0)
  • 2020-12-14 06:05
    double smallest = a;
    if (smallest > b) smallest = b;
    if (smallest > c) smallest = c;
    

    Not necessarily faster than your code.

    0 讨论(0)
  • 2020-12-14 06:06

    For pure characters-of-code efficiency, I can't find anything better than

    smallest = a<b&&a<c?a:b<c?b:c;
    
    0 讨论(0)
  • 2020-12-14 06:08

    For a lot of utility-type methods, the apache commons libraries have solid implementations that you can either leverage or get additional insight from. In this case, there is a method for finding the smallest of three doubles available in org.apache.commons.lang.math.NumberUtils. Their implementation is actually nearly identical to your initial thought:

    public static double min(double a, double b, double c) {
        return Math.min(Math.min(a, b), c);
    }
    
    0 讨论(0)
  • 2020-12-14 06:08

    For those who find this topic much later:

    If you have just three values to compare there is no significant difference. But if you have to find min of, say, thirty or sixty values, "min" could be easier for anyone to read in the code next year:

    int smallest;
    
    smallest = min(a1, a2);
    smallest = min(smallest, a3);
    smallest = min(smallest, a4);
    ...
    smallest = min(smallest, a37);
    

    But if you think of speed, maybe better way would be to put values into list, and then find min of that:

    List<Integer> mySet = Arrays.asList(a1, a2, a3, ..., a37);
    
    int smallest = Collections.min(mySet);
    

    Would you agree?

    0 讨论(0)
  • 2020-12-14 06:10

    Math.min uses a simple comparison to do its thing. The only advantage to not using Math.min is to save the extra function calls, but that is a negligible saving.

    If you have more than just three numbers, having a minimum method for any number of doubles might be valuable and would look something like:

    public static double min(double ... numbers) {
        double min = numbers[0];
        for (int i=1 ; i<numbers.length ; i++) {
            min = (min <= numbers[i]) ? min : numbers[i];
        }
        return min;
    }
    

    For three numbers this is the functional equivalent of Math.min(a, Math.min(b, c)); but you save one method invocation.

    0 讨论(0)
提交回复
热议问题