code polish
This commit is contained in:
parent
b2cbd9c084
commit
7822448427
|
@ -33,11 +33,13 @@ public abstract class BenchmarkReflection {
|
||||||
|
|
||||||
abstract int run(int iterations) throws Throwable;
|
abstract int run(int iterations) throws Throwable;
|
||||||
|
|
||||||
private BigDecimal time() {
|
private
|
||||||
|
BigDecimal time() {
|
||||||
try {
|
try {
|
||||||
int nextI = 1;
|
int nextI = 1;
|
||||||
int i;
|
int i;
|
||||||
long duration;
|
long duration;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
i = nextI;
|
i = nextI;
|
||||||
long start = System.nanoTime();
|
long start = System.nanoTime();
|
||||||
|
@ -45,6 +47,8 @@ public abstract class BenchmarkReflection {
|
||||||
duration = System.nanoTime() - start;
|
duration = System.nanoTime() - start;
|
||||||
nextI = i << 1 | 1;
|
nextI = i << 1 | 1;
|
||||||
} while (duration < 100000000 && nextI > 0);
|
} while (duration < 100000000 && nextI > 0);
|
||||||
|
|
||||||
|
|
||||||
return new BigDecimal(duration * 1000 / i).movePointLeft(3);
|
return new BigDecimal(duration * 1000 / i).movePointLeft(3);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
@ -75,20 +79,22 @@ public abstract class BenchmarkReflection {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
final C invocationTarget = new C();
|
final C invocationTarget = new C();
|
||||||
final Method m = C.class.getMethod("foo");
|
final Method m_normal = C.class.getMethod("foo");
|
||||||
final Method am = C.class.getMethod("foo");
|
|
||||||
am.setAccessible(true);
|
|
||||||
final MethodHandle mh = sfmh;
|
|
||||||
|
|
||||||
final MethodAccess ma = MethodAccess.get(C.class);
|
final Method m_setAccessible = C.class.getMethod("foo");
|
||||||
final int mi = ma.getIndex("foo");
|
m_setAccessible.setAccessible(true);
|
||||||
|
|
||||||
|
final MethodAccess m_asm = MethodAccess.get(C.class);
|
||||||
|
final int mi = m_asm.getIndex("foo");
|
||||||
|
|
||||||
|
final MethodHandle mh = sfmh;
|
||||||
|
|
||||||
BenchmarkReflection[] marks = {
|
BenchmarkReflection[] marks = {
|
||||||
new BenchmarkReflection("reflective invocation (without setAccessible)") {
|
new BenchmarkReflection("reflective invocation (without setAccessible)") {
|
||||||
@Override int run(int iterations) throws Throwable {
|
@Override int run(int iterations) throws Throwable {
|
||||||
int x = 0;
|
int x = 0;
|
||||||
for (int i = 0; i < iterations; i++) {
|
for (int i = 0; i < iterations; i++) {
|
||||||
x += (Integer) m.invoke(invocationTarget);
|
x += (Integer) m_normal.invoke(invocationTarget);
|
||||||
}
|
}
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
@ -97,7 +103,7 @@ public abstract class BenchmarkReflection {
|
||||||
@Override int run(int iterations) throws Throwable {
|
@Override int run(int iterations) throws Throwable {
|
||||||
int x = 0;
|
int x = 0;
|
||||||
for (int i = 0; i < iterations; i++) {
|
for (int i = 0; i < iterations; i++) {
|
||||||
x += (Integer) am.invoke(invocationTarget);
|
x += (Integer) m_setAccessible.invoke(invocationTarget);
|
||||||
}
|
}
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
@ -107,12 +113,12 @@ public abstract class BenchmarkReflection {
|
||||||
@Override int run(int iterations) throws Throwable {
|
@Override int run(int iterations) throws Throwable {
|
||||||
int x = 0;
|
int x = 0;
|
||||||
for (int i = 0; i < iterations; i++) {
|
for (int i = 0; i < iterations; i++) {
|
||||||
x += (Integer) ma.invoke(invocationTarget, mi, (Object)null);
|
x += (Integer) m_asm.invoke(invocationTarget, mi, (Object)null);
|
||||||
}
|
}
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
new BenchmarkReflection("methodhandle invocation") {
|
new BenchmarkReflection("methodhandle (local) invocation") {
|
||||||
|
|
||||||
@Override int run(int iterations) throws Throwable {
|
@Override int run(int iterations) throws Throwable {
|
||||||
int x = 0;
|
int x = 0;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user