java - 循环遍历二维数组。性能问题
问题描述
为什么下面未注释的代码比注释掉的代码快得多?这里是 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;
}
}
解决方案
区别
在您的第一种方法maximumWealth(int[][] acounts
中,您在内部和外部循环中使用传统的 for 循环,而在第二种方法中,您在外部循环中使用增强的 for 循环
for (int[] account : accounts)
实验
实验测量运行时间。
- 在 accounts 数组中创建了 21 个数组
- 使用两个不同的 maximumWealth(int[][] acounts) 循环遍历它
- 首先,我们使用增强的 for 循环
- 第二个我们使用传统的for循环
- 停止以纳秒为单位执行的时间。
- 执行步骤 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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, 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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, 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 循环。
资源:
推荐阅读
- ios - 有没有人有 iOS 中交换邮箱的演示?
- javascript - 使用时间作为 Chart.js 的 X 轴
- javascript - 未捕获的错误:您不应该使用
外面 , 但 Switch 在里面 - git - 如何使用 GIT 维护 2 个非常相似的项目?
- php - Laravel withCount() 子查询
- laravel - Laravel Collection:如何访问对象的对象?
- android - 如何检查 kotlin 已弃用的 API 使用情况
- matlab - 在matlab中将复数的虚部增加一个常数
- python - 我可以在 sagemaker 上部署预训练的 sklearn 模型(s3 中的泡菜)吗?
- javascript - 如何编写一个函数来查找数组中最大整数的索引?