jvm 会在每次执行时评估一个明显的 return 值吗?
Will jvm evaluate a obvious return value on each execution?
相信几乎每个人都熟悉most downvoted question(java tagged) on SO。复制粘贴代码段以确保完整性:
k = (j = (i = 0) + 2) + 1;
return i|= j|= k|= (j+= i) - - (k+++k) - - (i =+j);
以上代码段总是 returns 11 无论如何。所以我的问题是:jvm 会在每次调用时评估 this/similar 疯狂吗?
不知道这个算不算答案,不过好像JVM
可以证明不需要评价:
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 20, time = 20)
@Measurement(iterations = 20, time = 20)
public class MostDownVoted {
public static void main(String[] args) throws Exception {
Options opt = new OptionsBuilder()
.include(MostDownVoted.class.getSimpleName())
.build();
new Runner(opt).run();
}
@State(Scope.Benchmark)
public static class Holder {
int k;
int i;
int j;
@Setup(Level.Iteration)
public void setUp() {
k = ThreadLocalRandom.current().nextInt();
i = ThreadLocalRandom.current().nextInt();
j = ThreadLocalRandom.current().nextInt();
}
}
@Fork(1)
@Benchmark
public int test1(Holder h) {
h.k = (h.j = (h.i = 0) + 2) + 1;
return h.i |= h.j |= h.k |= (h.j += h.i) - -(h.k++ + h.k) - -(h.i = +h.j);
}
@Fork(1)
@Benchmark
public int test2(Holder h) {
return 11;
}
@Benchmark
@Fork(value = 1, jvmArgsAppend = "-XX:TieredStopAtLevel=1")
public int test3(Holder h) {
h.k = (h.j = (h.i = 0) + 2) + 1;
return h.i |= h.j |= h.k |= (h.j += h.i) - -(h.k++ + h.k) - -(h.i = +h.j);
}
}
结果表明,一旦 C2
编译器启动,return 11
的结果与您拥有的结果相当:
MostDownVoted.test1 avgt 20 2.816 ± 0.003 ns/op
MostDownVoted.test2 avgt 20 2.122 ± 0.016 ns/op
MostDownVoted.test3 avgt 20 3.979 ± 0.758 ns/op
相信几乎每个人都熟悉most downvoted question(java tagged) on SO。复制粘贴代码段以确保完整性:
k = (j = (i = 0) + 2) + 1;
return i|= j|= k|= (j+= i) - - (k+++k) - - (i =+j);
以上代码段总是 returns 11 无论如何。所以我的问题是:jvm 会在每次调用时评估 this/similar 疯狂吗?
不知道这个算不算答案,不过好像JVM
可以证明不需要评价:
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 20, time = 20)
@Measurement(iterations = 20, time = 20)
public class MostDownVoted {
public static void main(String[] args) throws Exception {
Options opt = new OptionsBuilder()
.include(MostDownVoted.class.getSimpleName())
.build();
new Runner(opt).run();
}
@State(Scope.Benchmark)
public static class Holder {
int k;
int i;
int j;
@Setup(Level.Iteration)
public void setUp() {
k = ThreadLocalRandom.current().nextInt();
i = ThreadLocalRandom.current().nextInt();
j = ThreadLocalRandom.current().nextInt();
}
}
@Fork(1)
@Benchmark
public int test1(Holder h) {
h.k = (h.j = (h.i = 0) + 2) + 1;
return h.i |= h.j |= h.k |= (h.j += h.i) - -(h.k++ + h.k) - -(h.i = +h.j);
}
@Fork(1)
@Benchmark
public int test2(Holder h) {
return 11;
}
@Benchmark
@Fork(value = 1, jvmArgsAppend = "-XX:TieredStopAtLevel=1")
public int test3(Holder h) {
h.k = (h.j = (h.i = 0) + 2) + 1;
return h.i |= h.j |= h.k |= (h.j += h.i) - -(h.k++ + h.k) - -(h.i = +h.j);
}
}
结果表明,一旦 C2
编译器启动,return 11
的结果与您拥有的结果相当:
MostDownVoted.test1 avgt 20 2.816 ± 0.003 ns/op
MostDownVoted.test2 avgt 20 2.122 ± 0.016 ns/op
MostDownVoted.test3 avgt 20 3.979 ± 0.758 ns/op