自分でテストできますが、配列の削除と再作成はほぼ同じです。
ただし、2つの欠点があります
- CPU データ キャッシュがスクロールし、その効果が低下します。
- GC をトリガーする可能性が高くなります。特にこれを頻繁に行うと、システムが一時停止するか、速度が低下します (同時実行の場合)。
私は配列を再利用することを好みます。これが最速だからではありませんが、アプリケーションの残りの部分への影響がはるかに少ないからです。
for (int size = 16; size <= 16* 1024; size *= 2) {
int count1 = 0, count1b = 0,count2 = 0;
long total1 = 0, total1b = 0, total2 = 0;
for (long i = 0; i < 10000000000L; i += size) {
long start = System.nanoTime();
long[] longs = new long[size];
if (longs[0] + longs[longs.length - 1] != 0)
throw new AssertionError();
long mid = System.nanoTime();
long time1 = mid - start;
Arrays.fill(longs, 1L);
long time2 = System.nanoTime() - mid;
count1b++;
total1b += time1;
if (time1 < 10e3) {// no GC
total1 += time1;
count1++;
}
if (time2 < 10e3) {// no GC
total2 += time2;
count2++;
}
}
System.out.printf("%s KB took on average of %,d ns to allocate, %,d ns to allocate including GCs and %,d ns to fill%n",
size * 8 / 1024.0, total1 / count1, total1b/count1b, total2 / count2);
}
版画
0.125 KB took on average of 35 ns to allocate, 36 ns to allocate including GCs and 19 ns to fill
0.25 KB took on average of 39 ns to allocate, 40 ns to allocate including GCs and 31 ns to fill
0.5 KB took on average of 56 ns to allocate, 58 ns to allocate including GCs and 55 ns to fill
1.0 KB took on average of 75 ns to allocate, 77 ns to allocate including GCs and 117 ns to fill
2.0 KB took on average of 129 ns to allocate, 134 ns to allocate including GCs and 232 ns to fill
4.0 KB took on average of 242 ns to allocate, 248 ns to allocate including GCs and 368 ns to fill
8.0 KB took on average of 479 ns to allocate, 496 ns to allocate including GCs and 644 ns to fill
16.0 KB took on average of 1,018 ns to allocate, 1,055 ns to allocate including GCs and 1,189 ns to fill
32.0 KB took on average of 2,119 ns to allocate, 2,200 ns to allocate including GCs and 2,625 ns to fill
64.0 KB took on average of 4,419 ns to allocate, 4,604 ns to allocate including GCs and 4,728 ns to fill
128.0 KB took on average of 8,333 ns to allocate, 9,472 ns to allocate including GCs and 8,685 ns to fill
すべての場合において、一方のアプローチが他方よりも高速であると仮定するのは難しいことを証明するだけです。
を に変更するlong[]
と、int[]
ほとんど同じことがわかります
0.125 KB took on average of 35 ns to allocate, 36 ns to allocate including GCs and 16 ns to fill
0.25 KB took on average of 40 ns to allocate, 41 ns to allocate including GCs and 24 ns to fill
0.5 KB took on average of 58 ns to allocate, 60 ns to allocate including GCs and 40 ns to fill
1.0 KB took on average of 86 ns to allocate, 87 ns to allocate including GCs and 94 ns to fill
2.0 KB took on average of 139 ns to allocate, 143 ns to allocate including GCs and 149 ns to fill
4.0 KB took on average of 256 ns to allocate, 262 ns to allocate including GCs and 206 ns to fill
8.0 KB took on average of 472 ns to allocate, 481 ns to allocate including GCs and 317 ns to fill
16.0 KB took on average of 981 ns to allocate, 999 ns to allocate including GCs and 516 ns to fill
32.0 KB took on average of 2,098 ns to allocate, 2,146 ns to allocate including GCs and 1,458 ns to fill
64.0 KB took on average of 4,312 ns to allocate, 4,445 ns to allocate including GCs and 4,028 ns to fill
128.0 KB took on average of 8,497 ns to allocate, 9,072 ns to allocate including GCs and 7,141 ns to fill