当前位置:网站首页>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 .
边栏推荐
- web3.0系列之分布式存储IPFS
- Programming features of ISP, IAP, ICP, JTAG and SWD
- Postman interface test V
- Some test points about coupon test
- Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
- 【acwing】789. 数的范围(二分基础)
- Leetcode exercise - 113 Path sum II
- Memory ==c language 1
- STM32 ADC和DMA
- [learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)
猜你喜欢
Some thoughts on the testing work in the process of R & D
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
Appx code signing Guide
ISP、IAP、ICP、JTAG、SWD的编程特点
Word自动生成目录的方法
Pdf document signature Guide
This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
Bean 作⽤域和⽣命周期
Pytest learning - dayone
Postman interface test VI
随机推荐
C#记录日志方法
Leetcode exercise - 113 Path sum II
Interface test
Parameter sniffing (1/2)
【ORM框架】
Inno setup packaging and signing Guide
Use of JSON extractor originals in JMeter
ORM -- grouping query, aggregation query, query set queryset object properties
ORM--数据库增删改查操作逻辑
Differences between MCU and MPU
Delete a record in the table in pl/sql by mistake, and the recovery method
Win10安装VS2015
HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
ES类和对象、原型
Become a "founder" and make reading a habit
2022.7.3DAY595
AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
【学习笔记-李宏毅】GAN(生成对抗网络)全系列(一)
[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)
China's first electronic audio category "Yamano electronic audio" digital collection is on sale!