dev*_*ull 1 java collections microbenchmark jmh
如果我知道那个点的大小,那么传递Collection给Collection构造函数的大小是否更好?扩展Collection和分配/重新分配的节约效果是否显着?
如果我知道最小尺寸Collection而不是上限,该怎么办?至少在最小尺寸的情况下仍然值得创造吗?
不同的集合对此有不同的性能影响,对于ArrayList,保存可以非常明显.
import java.util.*;
public class Main{
public static void main(String[] args){
List<Integer> numbers = new ArrayList<Integer>(5);
int max = 1000000;
// Warmup
for (int i=0;i<max;i++) {
numbers.add(i);
}
long start = System.currentTimeMillis();
numbers = new ArrayList<Integer>(max);
for (int i=0;i<max;i++) {
numbers.add(i);
}
System.out.println("Preall: "+(System.currentTimeMillis()-start));
start = System.currentTimeMillis();
numbers = new ArrayList<Integer>(5);
for (int i=0;i<max;i++) {
numbers.add(i);
}
System.out.println("Resizing: "+(System.currentTimeMillis()-start));
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
Preall: 26
Resizing: 58
Run Code Online (Sandbox Code Playgroud)
以max设置为10000000的值的10倍运行给出:
Preall: 510
Resizing: 935
Run Code Online (Sandbox Code Playgroud)
所以你甚至可以看到不同尺寸的比例保持不变.
这几乎是最糟糕的测试,但是一次填充一个数组元素非常常见,你可以看到速度差异大约为2*.
好的,这是我的 jmh 代码:
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 3, time = 1)
@Measurement(iterations = 3, time = 1)
@Fork(3)
public class Comparison
{
static final int size = 1_000;
@GenerateMicroBenchmark
public List<?> testSpecifiedSize() {
final ArrayList<Integer> l = new ArrayList(size);
for (int i = 0; i < size; i++) l.add(1);
return l;
}
@GenerateMicroBenchmark
public List<?> testDefaultSize() {
final ArrayList<Integer> l = new ArrayList();
for (int i = 0; i < size; i++) l.add(1);
return l;
}
}
Run Code Online (Sandbox Code Playgroud)
我的结果size = 10_000:
Benchmark Mode Thr Cnt Sec Mean Mean error Units
testDefaultSize avgt 1 9 1 80.770 2.095 usec/op
testSpecifiedSize avgt 1 9 1 50.060 1.078 usec/op
Run Code Online (Sandbox Code Playgroud)
结果size = 1_000:
Benchmark Mode Thr Cnt Sec Mean Mean error Units
testDefaultSize avgt 1 9 1 6.208 0.131 usec/op
testSpecifiedSize avgt 1 9 1 4.900 0.078 usec/op
Run Code Online (Sandbox Code Playgroud)
如果这让您感觉心脏周围更温暖,请添加初始尺寸,但客观地说,您的客户极不可能注意到差异。
| 归档时间: |
|
| 查看次数: |
3605 次 |
| 最近记录: |