我正在开发一个名为Banana的原始集合库,它支持原始链接列表。您的用例它几乎是 Banana 发光的理想用例,但它可以做更多的事情(包括可变长度块,您没有使用但可能在您的实际案例中使用)。
这是我电脑上这个基准测试的结果:
Banana : 1269 ms elapsed
Banana : total: 2.5 GB, free: 0.5 GB, used = 2.1 GB, Banana reports that it's actually using 2.1 GB
Java : 13543 ms elapsed
Java : total: 6.2 GB, free: 2.0 GB, used = 4.2 GB
您可以看到 Banana 速度更快,并且使用的内存更少。(Java内存如果不先运行banana函数自己运行会更好
Java : 14426 ms elapsed
Java : total: 5.8 GB, free: 1.9 GB, used = 3.9 GB
但仍然没有靠近香蕉。
package net.yadan.banana.list;
public class LinkedListBenchmark {
public static void main(String[] args) {
banana();
java();
}
public static void banana() {
long t = System.currentTimeMillis();
// initial list size 16m records, block size 32 (storage is int[], so we
// need 32 ints to hold 16 longs)
net.yadan.banana.list.LinkedList list = new LinkedList(16 * 1024 * 1024, 16 * 2, 0);
// initial fill in
for (int i = 0; i < 16L * 1024 * 1024; i++) {
list.appendTail(32); // similar to java list.add() which appends to the
// end of the list
}
// the main stuff
for (int i = 0; i < 16L * 1024 * 1024; i++) {
list.removeHead(); // similar to java list removeFirst()
list.appendTail(32); // similar to java list.add() which appends to the
// end of the list
}
System.out.println("Banana : " + (System.currentTimeMillis() - t) + " ms elapsed");
float GB = 1024 * 1024 * 1024;
long total = Runtime.getRuntime().totalMemory();
long free = Runtime.getRuntime().freeMemory();
System.out
.printf(
"Banana : total: %5.1f GB, free: %5.1f GB, used = %5.1f GB, Banana reports that it's actually using %5.1f GB\n",
total / GB, free / GB, (total - free) / GB, list.computeMemoryUsage() / GB);
}
public static void java() {
long t = System.currentTimeMillis();
java.util.LinkedList<long[]> list = new java.util.LinkedList<long[]>();
// initial fill in
for (int i = 0; i < 16L * 1024 * 1024; i++) {
list.add(new long[16]);
}
// the main stuff
for (int i = 0; i < 16L * 1024 * 1024; i++) {
list.removeFirst();
list.add(new long[16]);
}
System.out.println("Java : " + (System.currentTimeMillis() - t) + " ms elapsed");
float GB = 1024 * 1024 * 1024;
long total = Runtime.getRuntime().totalMemory();
long free = Runtime.getRuntime().freeMemory();
System.out.printf("Java : total: %5.1f GB, free: %5.1f GB, used = %5.1f GB\n", total / GB, free / GB,
(total - free) / GB);
}
}