Java反射测试

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
---------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值