为什么二维数组 运行 比 java 中的一维数组慢很多
Why 2D arrays run significantly slower than 1D arrays in java
对于我的一个程序我需要大内存,我用两种不同的实现来做到这一点,它们如下:
int SIZE = 1000000000;
int[] rnums = new int[SIZE];
byte[] d1 = new byte[2 * SIZE];
byte[] d2 = new byte[2 * SIZE];
int SIZE = 1000000000;
int[] rnums = new int[SIZE];
byte[][] d1 = new byte[SIZE][2];
byte[][] d2 = new byte[SIZE][2];
两个程序都可以工作并产生正确的答案,但是 2D 实现非常慢,随着 SIZE 的增加,它变得越来越慢。
其余代码非常相似,我不明白为什么 2D 会导致那么多延迟。
按照@David Zimmerman 的建议,我已将代码更改为以下代码:
int SIZE = 1000000000;
int[] rnums = new int[SIZE];
byte[][] d1 = new byte[2][SIZE];
byte[][] d2 = new byte[2][SIZE];
它运行正常。
对于我的一个程序我需要大内存,我用两种不同的实现来做到这一点,它们如下:
int SIZE = 1000000000;
int[] rnums = new int[SIZE];
byte[] d1 = new byte[2 * SIZE];
byte[] d2 = new byte[2 * SIZE];
int SIZE = 1000000000;
int[] rnums = new int[SIZE];
byte[][] d1 = new byte[SIZE][2];
byte[][] d2 = new byte[SIZE][2];
两个程序都可以工作并产生正确的答案,但是 2D 实现非常慢,随着 SIZE 的增加,它变得越来越慢。
其余代码非常相似,我不明白为什么 2D 会导致那么多延迟。
按照@David Zimmerman 的建议,我已将代码更改为以下代码:
int SIZE = 1000000000;
int[] rnums = new int[SIZE];
byte[][] d1 = new byte[2][SIZE];
byte[][] d2 = new byte[2][SIZE];
它运行正常。