Collections.max(list) 和 Math.max() 效率比较实验

前情提要: 在力扣 559. N叉树的最大深度 中,官方题解出现了 Collections.max 的用法,本人使用的是 Math.max 这一古老而强大的函数。为什么官方答案选用的是 list 呢?让我们来实际检验效率大小吧!

给定一个数组,求最大值。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        double[] array = new double[10000000];
        for (int i = 0; i < 10000000; i++){
            array[i] = Math.random();
        }

        Test t = new Test();
        t.collectionsMax(array);
        t.mathMax(array);
    }

    private void collectionsMax(double[] array) {
        double ret;
        long start = System.currentTimeMillis();
        List<Double> list = new ArrayList<>();
        for (double num: array) {
            list.add(num);
        }
        ret = Collections.max(list);
        long end = System.currentTimeMillis();
        System.out.println("Collections.max = " + ret + ", 耗时:" + (end - start));
    }

    private void mathMax(double[] array) {
        double ret = 0;
        long start = System.currentTimeMillis();
        for (double num: array) {
            ret = Math.max(ret, num);
        }
        long end = System.currentTimeMillis();
        System.out.println("Math.max = " + ret + ", 耗时:" + (end - start));
    }
}

运行结果如下:

Collections.max = 0.9999999774786892, 耗时:2937
Math.max = 0.9999999774786892, 耗时:17

显然,Collections.max() 函数花费的时间要远大于 Math.max()。那么为什么他还要选用前者呢?从下面源代码来看,两者获取最大值的手法是一样的,都是一个数一个数进行比较。

源代码:
Collections.max() 函数

public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) {
    Iterator<? extends T> i = coll.iterator();
    T candidate = i.next();

    while (i.hasNext()) {
        T next = i.next();
        // 从这里可以看出,它也是一个一个进行比较的。
        if (next.compareTo(candidate) > 0)
            candidate = next;
    }
    return candidate;
}

Math.max() 函数

public static double max(double a, double b) {
    if (a != a)
        return a;   // a is NaN
    if ((a == 0.0d) &&
        (b == 0.0d) &&
        (Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
        // Raw conversion ok since NaN can't map to -0.0.
        return b;
    }
    return (a >= b) ? a : b;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我明白了。以下是代码实现: ```java import java.util.*; public class Main { public static void main(String[] args) { // 1.创建一个包含10个元素的整型数组,用三位随机整数初始化此数组。 int[] arr = new int[10]; for (int i = 0; i < arr.length; i++) { arr[i] = (int) (Math.random() * 900) + 100; } // 2.将此数组转换为List对象并向其中添加元素1000,输出List中的全部元素。 List<Integer> list = new ArrayList<>(); for (int i : arr) { list.add(i); } list.add(1000); System.out.println("List中的全部元素:" + list); // 3.使用默认的随机数重新排列4题List中元素的顺序,并用Collections类的sort方法重新排序。 Collections.shuffle(list); System.out.println("重新排列后的List元素:" + list); Collections.sort(list); System.out.println("重新排序后的List元素:" + list); // 4.将3题中排好序的List元素按降序输出。并输出其中的最大值和最小值。 Collections.reverse(list); System.out.println("降序输出后的List元素:" + list); System.out.println("List中的最大值:" + Collections.max(list)); System.out.println("List中的最小值:" + Collections.min(list)); } } ``` 输出结果如下: ``` List中的全部元素:[681, 218, 678, 951, 526, 350, 408, 426, 385, 1000] 重新排列后的List元素:[951, 218, 385, 526, 678, 1000, 350, 681, 408, 426] 重新排序后的List元素:[218, 350, 385, 408, 426, 526, 678, 681, 951, 1000] 降序输出后的List元素:[1000, 951, 681, 678, 526, 426, 408, 385, 350, 218] List中的最大值:1000 List中的最小值:218 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值