看起来 list.clear() 会更慢,
并非总是如此。
clear() 必须清除您使用的引用,但是当您创建一个新对象时,它必须清除该对象的内存才能使用它。
在第二种情况下,GC 将处理清理并使我们的生活变得轻松?
GC 不是免费的。当您用垃圾填充 CPU 缓存时,它们将无法正常工作。您可以通过重用对象显着加快代码速度。这取决于您的用例,哪个更快。
很难找到一个像样的微基准来证明这一点,但在代码更复杂的实际程序中,影响远高于您的预期。
public class Main {
public static void main(String... args) {
for (int i = 0; i < 10; i++) {
long t1 = timeReuse();
long t2 = timeNewObject();
System.out.printf("Reuse time: %,d, New ArrayList time: %,d%n", t1, t2);
}
}
static final int RUNS = 50000;
static final byte[] a = new byte[8 * 1024];
static final byte[] b = new byte[a.length];
private static long timeReuse() {
long start = System.nanoTime();
List<Integer> ints = new ArrayList<Integer>();
for (int i = 0; i < RUNS; i++) {
ints.clear();
for (int j = -128; j < 128; j++)
ints.add(j);
System.arraycopy(a, 0, b, 0, a.length);
}
long time = System.nanoTime() - start;
return time / RUNS;
}
private static long timeNewObject() {
long start = System.nanoTime();
for (int i = 0; i < RUNS; i++) {
List<Integer> ints = new ArrayList<Integer>(256);
for (int j = -128; j < 128; j++)
ints.add(j);
System.arraycopy(a, 0, b, 0, a.length);
}
long time = System.nanoTime() - start;
return time / RUNS;
}
}
打印
Reuse time: 1,964, New ArrayList time: 1,866
Reuse time: 1,889, New ArrayList time: 1,770
Reuse time: 1,163, New ArrayList time: 1,416
Reuse time: 1,250, New ArrayList time: 1,357
Reuse time: 1,253, New ArrayList time: 1,393
Reuse time: 1,106, New ArrayList time: 1,203
Reuse time: 1,103, New ArrayList time: 1,207
Reuse time: 1,113, New ArrayList time: 1,315
Reuse time: 1,104, New ArrayList time: 1,215
Reuse time: 1,106, New ArrayList time: 1,335
注意:复制的缓冲区大小会有所不同。
如果考虑延迟,情况会更糟。这会打印出每次运行的最差延迟。
public class Main {
public static void main(String... args) {
for (int i = 0; i < 10; i++) {
long t1 = timeReuse();
long t2 = timeNewObject();
System.out.printf("Reuse time: %,d, New ArrayList time: %,d%n", t1, t2);
}
}
static final int RUNS = 50000;
static final byte[] a = new byte[8 * 1024];
static final byte[] b = new byte[a.length];
private static long timeReuse() {
List<Integer> ints = new ArrayList<Integer>();
long longest = 0;
for (int i = 0; i < RUNS; i++) {
long start = System.nanoTime();
ints.clear();
for (int j = -128; j < 128; j++)
ints.add(j);
System.arraycopy(a, 0, b, 0, a.length);
long time = System.nanoTime() - start;
longest = Math.max(time, longest);
}
return longest;
}
private static long timeNewObject() {
long longest = 0;
for (int i = 0; i < RUNS; i++) {
long start = System.nanoTime();
List<Integer> ints = new ArrayList<Integer>(256);
for (int j = -128; j < 128; j++)
ints.add(j);
System.arraycopy(a, 0, b, 0, a.length);
long time = System.nanoTime() - start;
longest = Math.max(time, longest);
}
return longest;
}
}
使用-Xmx32m 运行时打印
Reuse time: 74,879, New ArrayList time: 2,441,586
Reuse time: 26,889, New ArrayList time: 2,203,096
Reuse time: 25,920, New ArrayList time: 1,514,465
Reuse time: 13,013, New ArrayList time: 1,342,395
Reuse time: 12,368, New ArrayList time: 1,708,658
Reuse time: 12,272, New ArrayList time: 1,258,990
Reuse time: 12,559, New ArrayList time: 1,433,898
Reuse time: 12,144, New ArrayList time: 1,259,413
Reuse time: 12,433, New ArrayList time: 1,221,945
Reuse time: 12,352, New ArrayList time: 1,318,024