当前位置:网站首页>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 .
边栏推荐
- 大整数类实现阶乘
- Some test points about coupon test
- Integer inversion
- How to cancel automatic saving of changes in sqlyog database
- MCU is the most popular science (ten thousand words summary, worth collecting)
- LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件
- Web3.0 series distributed storage IPFs
- .NET配置系统
- Guid primary key
- Postman interface test VI
猜你喜欢
随机推荐
Embedded background - chip
Web3.0 series distributed storage IPFs
ORM--分组查询,聚合查询,查询集QuerySet对象特性
【学习笔记-李宏毅】GAN(生成对抗网络)全系列(一)
Parameter sniffing (1/2)
STM32 product introduction
Bean 作⽤域和⽣命周期
MCU与MPU的区别
ES6中的函數進階學習
ES6中的原型对象
STM32 ADC and DMA
Postman interface test VI
Postman tutorial - scripting
STM32 ADC和DMA
ISP、IAP、ICP、JTAG、SWD的编程特点
ORM -- query type, association query
网上可以开炒股账户吗安全吗
反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
Smart city construction based on GIS 3D visualization technology
高数_第1章空间解析几何与向量代数_向量的数量积