标签:拆箱 性能 com mys 启动 volatil java thread return
一:for循环和java8的性能比较如下:
// 一个大的 ArrayList,内部是随机的整形数据
volatile List<Integer> integers = …
// 基准测试 1
public int forEachLoopMaxInteger() {
int max = Integer.MIN_VALUE;
for (Integer n : integers) {
max = Integer.max(max, n);
}
return max;
}
// 基准测试 2
public int lambdaMaxInteger() {
return integers.stream().reduce(Integer.MIN_VALUE, (a, b) -> Integer.max(a, b));
}
这里java8比较慢,原因是性能消耗在装箱,拆箱。java8启动较慢,不好调试
二:
微基准测试是底层开发者,对性能有极致需求的开发者。
比如:JMH测试
-XX:+PrintCompilation 代码预热
-Xbatch
public void testMethod() {
int left = 10;
int right = 100;
int mul = left * right;
}
-XX:+PrintInlining
public void testMethod(Blackhole blackhole) {
// …
blackhole.consume(mul);
}
@State(Scope.Thread)
public static class MyState {
public int left = 10;
public int right = 100;
}
public void testMethod(MyState state, Blackhole blackhole) {
int left = state.left;
int right = state.right;
int mul = left * right;
blackhole.consume(mul);
}
标签:拆箱 性能 com mys 启动 volatil java thread return
原文地址:https://www.cnblogs.com/hanguocai/p/10119579.html