Java反射测试代码
测试反射执行效率,包括直接调用,JDK反射,Reflectasmf反射调用。
需引用Java包
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>reflectasm</artifactId>
<version>1.11.7</version>
</dependency>
代码如下
// An highlighted block
import java.lang.reflect.Method;
import com.esotericsoftware.reflectasm.MethodAccess;
public class ReflectTest {
public static void main(String[] args) throws Exception {
// int loop = 1;
int loop = 1_000_000_0;
// 启动运行一段时间
for (int i = 0; i < loop; i++) {
TestClass c = new TestClass();
c.test(10, 100);
}
for (int i = 0; i < 3; i++) {
java(loop);
instance(loop);
executeMethod(loop);
executeReflectMethod(loop);
executeCacheMethod(loop);
executeAsmMethod(loop);
executeCacheAsmMethod(loop);
System.out.println("---------------------------------------------------");
}
}
public static void executeAsmMethod(int loop) {
long s3 = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
TestClass c = new TestClass();
MethodAccess access = MethodAccess.get(TestClass.class);
access.invoke(c, "test", 10, 100);
}
long e3 = System.currentTimeMillis();
log("asm执行方法", s3, e3);
}
public static void executeCacheAsmMethod(int loop) {
long s3 = System.currentTimeMillis();
MethodAccess access = MethodAccess.get(TestClass.class);
for (int i = 0; i < loop; i++) {
TestClass c = new TestClass();
access.invoke(c, "test", 10, 100);
}
long e3 = System.currentTimeMillis();
log("缓存asm执行方法", s3, e3);
}
public static void executeCacheMethod(int loop) throws Exception {
long s3 = System.currentTimeMillis();
Method[] ms = TestClass.class.getDeclaredMethods();
for (int i = 0; i < loop; i++) {
TestClass c = new TestClass();
ms[0].invoke(c, 10, 100);
}
long e3 = System.currentTimeMillis();
log("缓存执行反射方法", s3, e3);
}
public static void executeReflectMethod(int loop) throws Exception {
long s3 = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
Method[] ms = TestClass.class.getDeclaredMethods();
TestClass c = TestClass.class.newInstance();
ms[0].invoke(c, 10, 100);
}
long e3 = System.currentTimeMillis();
log("反射对象、方法", s3, e3);
}
public static void executeMethod(int loop) throws Exception {
long s3 = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
Method[] ms = TestClass.class.getDeclaredMethods();
TestClass c = new TestClass();
ms[0].invoke(c, 10, 100);
}
long e3 = System.currentTimeMillis();
log("直接创建,反射方法", s3, e3);
}
public static void instance(int loop) throws Exception {
long s2 = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
TestClass c1 = TestClass.class.newInstance();
c1.test(10, 100);
}
long e2 = System.currentTimeMillis();
log("反射对象", s2, e2);
}
private static void java(int loop) {
long s1 = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
TestClass c = new TestClass();
c.test(10, 100);
}
long e1 = System.currentTimeMillis();
log("直接调用", s1, e1);
}
private static void log(String ss, long s, long e) {
// System.out.println(String.format("%-50s%s%s", ss, "运行消耗: ", e - s));
System.out.println(String.format(String.format("%-" + (50 - (ss.getBytes().length - ss.length()) * 3) + "s%s%d", ss, "运行消耗: ", e - s)));
}
}
class TestClass {
public double test(int x, int y) {
double z = Math.atan2(y, x);
return z;
}
}
运行结果
直接调用 运行消耗: 631
反射对象 运行消耗: 657
直接创建,反射方法 运行消耗: 1364
反射对象、方法 运行消耗: 1305
缓存执行反射方法 运行消耗: 688
asm执行方法 运行消耗: 10575
缓存asm执行方法 运行消耗: 667
---------------------------------------------------
直接调用 运行消耗: 644
反射对象 运行消耗: 660
直接创建,反射方法 运行消耗: 1285
反射对象、方法 运行消耗: 1233
缓存执行反射方法 运行消耗: 694
asm执行方法 运行消耗: 10813
缓存asm执行方法 运行消耗: 649
---------------------------------------------------
直接调用 运行消耗: 631
反射对象 运行消耗: 688
直接创建,反射方法 运行消耗: 1347
反射对象、方法 运行消耗: 1491
缓存执行反射方法 运行消耗: 764
asm执行方法 运行消耗: 11186
缓存asm执行方法 运行消耗: 674
---------------------------------------------------