首页 > 解决方案 > 循环遍历二维数组。性能问题

问题描述

为什么下面未注释的代码比注释掉的代码快得多?这里是 Leetcode 初学者,老实说,我看不出两者之间的区别。

class Solution {
    //public int maximumWealth(int[][] accounts) {
    //    int max = 0;
    //    for (int i = 0; i < accounts.length; i++) {
    //        int sum = 0;
    //        for (int j = 0; j < accounts[i].length; j++) {
    //            sum += accounts[i][j];
    //        }
    //        max = Integer.max(max, sum);
    //    }
    //    return max;
    //}
    public int maximumWealth(int[][] accounts) {
        int max = 0;
        for (int[] account : accounts) {
            int sum = 0;
            for (int j = 0; j < account.length; j++) {
                sum += account[j];
            }
            max = Integer.max(max, sum);
        }
        return max;
    }
}

标签: javaarraysperformanceruntime

解决方案


区别

在您的第一种方法maximumWealth(int[][] acounts中,您在内部外部循环中使用传统的 for 循环,而在第二种方法中,您在外部循环中使用增强的 for 循环

 for (int[] account : accounts)

实验

实验测量运行时间。

  1. 在 accounts 数组中创建了 21 个数组
  2. 使用两个不同的 maximumWealth(int[][] acounts) 循环遍历它
  • 首先,我们使用增强的 for 循环
  • 第二个我们使用传统的for循环
  1. 停止以纳秒为单位执行的时间。
  2. 执行步骤 3 十次并计算平均值
  3. 以纳秒为单位比较平均值

哪个循环更快?

增强的 for 循环

public class Tryout {
    public static void main(String[] args) {
        int[][] accounts = {{3, 3, 5, 5, 4, 2, 2, 3, 1, 31, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2},
                {2, 3, 3, 3, 33, 3, 3, 3, 3, 3, 3, 3, 33, 32, 2, 21, 1, 1, 22, 3, 3, 3, 4, 4, 4, 4, 9, 8, 2, 3, 4},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {5, 0, 2, 92, 31},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5}};
        long startTime = System.nanoTime();
        int result = Tryout.maximumWealthEnhanced(accounts);
        long endTime = System.nanoTime();
        long totalTime = endTime - startTime;
        System.out.println(totalTime);
    }

    public static int maximumWealthEnhanced(int[][] accounts) {
        int max = 0;
        for (int[] account : accounts) {
            int sum = 0;
            for (int j = 0; j < account.length; j++) {
                sum += account[j];
            }
            max = Integer.max(max, sum);
        }
        return max;
    }
}

运行时平均

1. 37085 NS
2. 41154 NS
3. 47630 NS
4. 37348 NS
5. 39489 NS
6. 37107 NS
7. 37605 NS
8. 37096 NS
9. 37609 NS
10. 39255 NS

AVG = 39137,0 纳秒

传统的for循环

public class Tryout {
    public static void main(String[] args) {
        int[][] accounts = {{3, 3, 5, 5, 4, 2, 2, 3, 1, 31, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2},
                {2, 3, 3, 3, 33, 3, 3, 3, 3, 3, 3, 3, 33, 32, 2, 21, 1, 1, 22, 3, 3, 3, 4, 4, 4, 4, 9, 8, 2, 3, 4},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {5, 0, 2, 92, 31},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5},
                {92, 92, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5}};
        long startTime = System.nanoTime();
        int result = Tryout.maximumWealth(accounts);
        long endTime = System.nanoTime();
        long totalTime = endTime - startTime;
        System.out.println(totalTime);
    }

    public static int maximumWealth(int[][] accounts) {
        int max = 0;
        for (int i = 0; i < accounts.length; i++) {
            int sum = 0;
            for (int j = 0; j < accounts[i].length; j++) {
                sum += accounts[i][j];
            }
            max = Integer.max(max, sum);
        }
        return max;
    }
}

运行时平均

1. 43645 NS
2. 42052 NS
3. 40661 NS
4. 40936 NS
5. 46346 NS
6. 46916 NS
7. 42064 NS
8. 39406 NS
9. 39572 NS
10. 40945 NS

AVG = 42254,3 纳秒


结论

所以我不会说增强版更快。我会说它快了一点纳秒。

这里要注意的重要一点是增强的 for 循环使用迭代器,因此如果您使用迭代器手动迭代集合,那么您应该具有几乎相同的性能。

增强型 for 循环的好处是方便且不易出错,但如果您想更好地控制迭代过程,请使用传统的 for 循环。

资源:

  1. Java中for循环和增强型for循环的区别
  2. 为什么增强的for循环比普通的for循环效率更高

推荐阅读