当前位置:网站首页>Why is the reflection efficiency low?
Why is the reflection efficiency low?
2022-07-07 10:18:00 【Flying stone】
Why is the reflection efficiency low
Recently spring Source code related content , notice aop The implementation of the , Among them, dynamic proxy involves reflection content , There are always Posts saying that reflection efficiency is low , So I want to verify whether the reflection efficiency is low at the whole point , And how low ? Whether it will affect the performance as long as it is used ?
Verify reflection efficiency
Code
Entity class
package org.springframework.test.reflect;
public class ReflectA {
private int age;
public String name;
private String sex;
public ReflectA(int age, String name, String sex) {
this.age = age;
this.name = name;
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
Test class
package org.springframework.test.reflect;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Test reflection efficiency
*/
public class TestEfficiency {
private static final int AVERAGE_COUNT = 10;
private static final int executeCount = 1000;
public static void main(String[] args) {
long costTime = 0;
long reflectMethodCostTime=0,normalMethodCostTime=0,reflectFieldCostTime=0,normalFieldCostTime=0;
for(int index = 0; index < AVERAGE_COUNT; index++){
costTime = getNormalCallCostTime(executeCount);
reflectMethodCostTime += costTime;
costTime = getReflectCallMethodCostTime(executeCount);
normalMethodCostTime += costTime;
costTime = getNormalFieldCostTime(executeCount);
reflectFieldCostTime += costTime;
costTime = getReflectCallFieldCostTime(executeCount);
normalFieldCostTime += costTime;
}
System.out.println("reflectMethodCostTime: " + reflectMethodCostTime/AVERAGE_COUNT + " ms");
System.out.println("normalMethodCostTime: " + normalMethodCostTime/AVERAGE_COUNT + " ms");
System.out.println("reflectFieldCostTime: " + reflectFieldCostTime/AVERAGE_COUNT + " ms");
System.out.println("normalFieldCostTime: " + normalFieldCostTime/AVERAGE_COUNT + " ms");
}
private static long getReflectCallMethodCostTime(int count){
long startTime = System.currentTimeMillis();
for(int index = 0 ; index < count; index++){
ReflectA a = new ReflectA(12," Xiao Ming ", " male ");
try{
Method setName = a.getClass().getMethod("setName", String.class);
setName.setAccessible(true);
setName.invoke(a, " Zhang San ");
}catch(IllegalAccessException e){
e.printStackTrace();
}catch(InvocationTargetException e){
e.printStackTrace();
}catch(NoSuchMethodException e){
e.printStackTrace();
}
}
return System.currentTimeMillis()-startTime;
}
private static long getReflectCallFieldCostTime(int count){
long startTime = System.currentTimeMillis();
for(int index = 0 ; index < count; index++){
ReflectA a = new ReflectA(12," Xiao Ming ", " male ");
try{
Field ageField = a.getClass().getDeclaredField("name");
ageField.set(a, " Li Si ");
}catch(NoSuchFieldException e){
e.printStackTrace();
}catch(IllegalAccessException e){
e.printStackTrace();
}
}
return System.currentTimeMillis()-startTime;
}
private static long getNormalCallCostTime(int count){
long startTime = System.currentTimeMillis();
for(int index = 0 ; index < count; index++){
ReflectA a = new ReflectA(12," Xiao Ming ", " male ");
a.setName(" Wang Wu ");
}
return System.currentTimeMillis()-startTime;
}
private static long getNormalFieldCostTime(int count){
long startTime = System.currentTimeMillis();
for(int index = 0 ; index < count; index++){
ReflectA a = new ReflectA(12," Xiao Ming ", " male ");
a.name = " Hou Liu ";
}
return System.currentTimeMillis()-startTime;
}
}
Output conclusion
Reflection 10 Time
- reflectMethodCostTime: 0 ms
- normalMethodCostTime: 0 ms
- reflectFieldCostTime: 0 ms
- normalFieldCostTime: 0 ms
Reflection 100 Time
- reflectMethodCostTime: 0 ms
- normalMethodCostTime: 0 ms
- reflectFieldCostTime: 0 ms
- normalFieldCostTime: 0 ms
Reflection 1000 Time
- reflectMethodCostTime: 1 ms
- normalMethodCostTime: 0 ms
- reflectFieldCostTime: 1 ms
- normalFieldCostTime: 0 ms
Reflection 10000 Time
- reflectMethodCostTime: 7 ms
- normalMethodCostTime: 0 ms
- reflectFieldCostTime: 5 ms
- normalFieldCostTime: 0 ms
Reflection 100000 Time
- reflectMethodCostTime: 23 ms
- normalMethodCostTime: 1 ms
- reflectFieldCostTime: 17 ms
- normalFieldCostTime: 1 ms
Reflection 1000000 Time
- reflectMethodCostTime: 77 ms
- normalMethodCostTime: 2 ms
- reflectFieldCostTime: 31 ms
- normalFieldCostTime: 2 ms
Conclusion
- 1. Reflection is indeed less efficient than normal access
- 2. Reflected in 100 within , The impact on performance can be ignored , Reflection 100-1000 There is a threshold of reflection affecting efficiency between times
- 3. Overall, it is more efficient to access attributes directly and through methods , The second is reflection acquisition properties , Finally, the reflection acquisition method .
边栏推荐
- This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
- Pdf document signature Guide
- Horizontal split of database
- Win10安装VS2015
- A wave of open source notebooks is coming
- 反射效率为什么低?
- The method of word automatically generating directory
- ORM模型--关联字段,抽象模型类
- Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
- Some test points about coupon test
猜你喜欢
官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系
ArcGIS operation: batch modify attribute table
The request object parses the request body and request header parameters
[ORM framework]
Word自动生成目录的方法
喜马拉雅网页版每次暂停后弹窗推荐下载客户端解决办法
Pdf document signature Guide
Leetcode exercise - 113 Path sum II
The Himalaya web version will pop up after each pause. It is recommended to download the client solution
随机推荐
ES6中的原型对象
Enterprise practice | construction of banking operation and maintenance index system under complex business relations
VS Code指定扩展安装位置
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
嵌入式背景知识-芯片
MongoDB创建一个隐式数据库用作练习
Leetcode exercise - 113 Path sum II
Pytest learning - dayone
SolidWorks工程图中添加中心线和中心符号线的办法
Google Colab装载Google Drive(Google Colab中使用Google Drive)
【学习笔记-李宏毅】GAN(生成对抗网络)全系列(一)
【acwing】786. Number k
电表远程抄表拉合闸操作命令指令
Why does the starting service report an error when installing MySQL? (operating system Windows)
【剑指Offer】42. 栈的压入、弹出序列
Official media attention! The list of top 100 domestic digital collection platforms was released, and the industry accelerated the healthy development of compliance
The request object parses the request body and request header parameters
This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
The method of word automatically generating directory
Integer inversion