当前位置:网站首页>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 .
边栏推荐
- CONDA creates virtual environment offline
- C logging method
- Download Text, pictures and ab packages used by unitywebrequest Foundation
- ISP、IAP、ICP、JTAG、SWD的编程特点
- ORM -- database addition, deletion, modification and query operation logic
- AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
- 【学习笔记-李宏毅】GAN(生成对抗网络)全系列(一)
- MongoDB创建一个隐式数据库用作练习
- ORM模型--关联字段,抽象模型类
- ES6中的函數進階學習
猜你喜欢
request对象对请求体,请求头参数的解析
Programming features of ISP, IAP, ICP, JTAG and SWD
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
中国首款电音音频类“山野电音”数藏发售来了!
The request object parses the request body and request header parameters
官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
Bean 作⽤域和⽣命周期
反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
随机推荐
Parameter sniffing (1/2)
Enterprise practice | construction of banking operation and maintenance index system under complex business relations
Download Text, pictures and ab packages used by unitywebrequest Foundation
基于gis三维可视化技术的智慧城市建设
ES6中的函数进阶学习
The new activity of "the arrival of twelve constellations and goddesses" was launched
Deadlock caused by non clustered index in SQL Server
IPv4 socket address structure
LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件
【acwing】789. 数的范围(二分基础)
Why does the starting service report an error when installing MySQL? (operating system Windows)
Use of JSON extractor originals in JMeter
ORM model -- associated fields, abstract model classes
ORM model -- creation and query of data records
Inno Setup 打包及签名指南
Introduction to automated testing framework
ORM--查询类型,关联查询
网上可以开炒股账户吗安全吗
对存储过程进行加密和解密(SQL 2008/SQL 2012)
ArcGIS operation: converting DWG data to SHP data