我正在研究在Java中用堆添加值的不同可能性。我正在使用PriorityHeap
课程。当我注意到我的应用程序运行缓慢时,我决定试一试。我添加了几千个,有时还有数百万个自定义条目(我有一个自定义类有3个字段:一个int,一个LongWritable和Text,都来自hadoop.io; this检测代理说我的记录有平均200字节)。
显而易见的是,使用addAll()
而不是add()
方法将条目放入堆中会提高性能,因为这会避免多次heapify
操作吗?
我使用以下新示例尝试了不同的策略:
package Sorting;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
public class Main {
private static final int HEAP_SIZE = 1000000;
private static final int BULK_LIST_SIZE = HEAP_SIZE / 10;
private static String normal;
private static String bulk;
private static String fullBulk;
public static void main(String[] args) throws IOException {
normal = "";
bulk = "";
fullBulk = "";
long time = 0;
warmup();
normal = "";
bulk = "";
fullBulk = "";
for (int i = 0; i < 100; i++) {
// Normal add time
System.out.println("Normal starts...");
time = normalExecution();
System.out.println("Normal add time " + time);
// Bulk add time
System.out.println("Bulk starts...");
time = bulk();
System.out.println("Bulk add time " + time);
// Bulk add time with list and heap with same size
System.out.println("Full Bulk starts...");
time = fullBulk();
System.out.println("Full Bulk add time " + time);
}
System.out.println(normal);
System.out.println(bulk);
System.out.println(fullBulk);
}
private static long fullBulk() {
long time;
long start;
List<Double> fullBulkList = new ArrayList<Double>(HEAP_SIZE);
PriorityQueue<Double> fullBulkHeap = new PriorityQueue<Double>(HEAP_SIZE);
start = System.nanoTime();
for (int j = 0; j < HEAP_SIZE; j++) {
if (fullBulkList.size() == HEAP_SIZE) {
fullBulkHeap.addAll(fullBulkList);
fullBulkList.clear();
}
}
fullBulkHeap.addAll(fullBulkList);
time = System.nanoTime() - start;
fullBulk = fullBulk + "\t" + time;
fullBulkList = null;
fullBulkHeap = null;
return time;
}
private static long bulk() {
long time;
long start;
List<Double> bulkList = new ArrayList<Double>(BULK_LIST_SIZE);
PriorityQueue<Double> bulkHeap = new PriorityQueue<Double>(HEAP_SIZE);
start = System.nanoTime();
for (int j = 0; j < HEAP_SIZE; j++) {
if (bulkList.size() == BULK_LIST_SIZE) {
bulkHeap.addAll(bulkList);
bulkList.clear();
}
}
bulkHeap.addAll(bulkList);
time = System.nanoTime() - start;
bulk = bulk + "\t" + time;
bulkList = null;
bulkHeap = null;
return time;
}
private static long normalExecution() {
long time;
long start;
PriorityQueue<Double> normalHeap = new PriorityQueue<Double>(HEAP_SIZE);
start = System.nanoTime();
for (int j = 0; j < HEAP_SIZE; j++) {
normalHeap.add(Double.MAX_VALUE);
}
time = System.nanoTime() - start;
normal = normal + "\t" + time;
normalHeap = null;
return time;
}
private static void warmup() {
System.out.println("Starting warmup");
for (int i = 0; i < 1000; i++) {
normalExecution();
bulk();
fullBulk();
}
for (int i = 0; i < 1000; i++) {
bulk();
fullBulk();
normalExecution();
}
for (int i = 0; i < 1000; i++) {
fullBulk();
normalExecution();
bulk();
}
System.out.println("Warmup finished");
}
}
结果产生了以下结果:
正常添加方法的第11次迭代中的巨大峰值由GC调用解释:[GC 1347684K->31354K(1446400K), 0.0331610 secs]
。
Mediam值分别为16049669,783724和800276。 ST dev分别为3512492.89,244374.17和33344.17。
答案 0 :(得分:2)
PriorityQueue
不会覆盖从addAll
继承的方法AbstractQueue
。
在AbstractQueue
中,此方法如下所示。
public boolean addAll(Collection<? extends E> c) {
if (c == null)
throw new NullPointerException();
if (c == this)
throw new IllegalArgumentException();
boolean modified = false;
for (E e : c)
if (add(e))
modified = true;
return modified;
}
如您所见,它只是循环并调用add
。
因此,与addAll
相比,我认为add
不会改善任何事情。