Skip to content

Instantly share code, notes, and snippets.

@xhumanoid
Created June 10, 2013 19:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xhumanoid/5751722 to your computer and use it in GitHub Desktop.
Save xhumanoid/5751722 to your computer and use it in GitHub Desktop.
java_bug
package test;
import org.openjdk.jmh.annotations.BenchmarkType;
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
import org.openjdk.jmh.annotations.OperationsPerInvocation;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import java.util.concurrent.TimeUnit;
public class TestInlining {
private static final int ARRAY_LENGTH = 100000;
public static final class FieldArrayTest {
public int value;
void inc() {
value++;
}
}
public static FieldArrayTest[] testFieldDirect(int incCount) {
FieldArrayTest[] a = new FieldArrayTest[ARRAY_LENGTH];
for (int i = 0; i < ARRAY_LENGTH; i++) {
a[i] = new FieldArrayTest();
}
while (incCount-- > 0) {
for (int i = 0; i < ARRAY_LENGTH; i++) {
a[i].value++;
}
}
return a;
}
public static FieldArrayTest[] testFieldCall(int incCount) {
FieldArrayTest[] a = new FieldArrayTest[ARRAY_LENGTH];
for (int i = 0; i < ARRAY_LENGTH; i++) {
a[i] = new FieldArrayTest();
}
while (incCount-- > 0) {
for (int i = 0; i < ARRAY_LENGTH; i++) {
a[i].inc();
}
}
return a;
}
public static FieldArrayTest[] fill() {
FieldArrayTest[] a = new FieldArrayTest[ARRAY_LENGTH];
for (int i = 0; i < ARRAY_LENGTH; i++) {
a[i] = new FieldArrayTest();
}
return a;
}
public static Object[] testFieldCall2(FieldArrayTest[] a, int incCount) {
while (incCount-- > 0) {
for (int i = 0; i < ARRAY_LENGTH; i++) {
a[i].inc();
}
}
return a;
}
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@GenerateMicroBenchmark(BenchmarkType.AverageTimePerOp)
public Object[] testFieldDirect() {
return testFieldDirect(1000);
}
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@GenerateMicroBenchmark(BenchmarkType.AverageTimePerOp)
public Object[] incrementnFieldCall() {
return testFieldCall(1000);
}
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@GenerateMicroBenchmark(BenchmarkType.AverageTimePerOp)
public Object[] incrementnFieldCall2() {
FieldArrayTest[] a = fill();
return testFieldCall2(a, 1000);
}
}
Benchmark Thr Cnt Sec Mean Mean error Var Units
t.g.a.TestInlining.incrementnFieldCall 1 10 1 450.429 6.500 39.999 msec/op
t.g.a.TestInlining.incrementnFieldCall2 1 10 1 110.059 2.148 4.369 msec/op
t.g.a.TestInlining.testFieldDirect 1 10 1 108.779 0.720 0.491 msec/op
Дальнейшие небольшие эксперименты и пачка различных запусков показало,
что даже t.g.a.TestInlining.incrementnFieldCall каждый 9-10 запуск может выдавать скорость около 110 msec/op.
@shipilev
Copy link

https://github.com/shipilev/benchmarks-scratch/blob/master/src/main/java/org/sample/TestInlining.java

Benchmark                                 Mode Thr    Cnt  Sec         Mean   Mean error    Units
o.s.TestInlining.incrementnFieldCall      avgt   1      5    1      164.252        3.330  msec/op
o.s.TestInlining.incrementnFieldCall2     avgt   1      5    1      164.254        4.717  msec/op
o.s.TestInlining.testFieldDirect          avgt   1      5    1      165.744        5.366  msec/op

@shipilev
Copy link

The same, with -f 10:

Benchmark                                 Mode Thr    Cnt  Sec         Mean   Mean error    Units
o.s.TestInlining.incrementnFieldCall      avgt   1     50    1      619.174       57.914  msec/op
o.s.TestInlining.incrementnFieldCall2     avgt   1     50    1      166.097        0.952  msec/op
o.s.TestInlining.testFieldDirect          avgt   1     50    1      166.813        1.298  msec/op

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment