当前位置:网站首页>反射效率为什么低?
反射效率为什么低?
2022-07-07 07:53:00 【会飞的小石头】
反射效率为什么低
最近看spring源码相关内容,看到aop的实现,其中有动态代理涉及到反射内容,总有帖子说反射效率低,所以就想验证下反射效率是否整点低,以及低到什么程度?是否只要使用就很影响性能?
验证反射效率
代码
实体类
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;
}
}
测试类
package org.springframework.test.reflect;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 测试反射效率
*/
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,"小明", "男");
try{
Method setName = a.getClass().getMethod("setName", String.class);
setName.setAccessible(true);
setName.invoke(a, "张三");
}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,"小明", "男");
try{
Field ageField = a.getClass().getDeclaredField("name");
ageField.set(a, "李四");
}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,"小明", "男");
a.setName("王五");
}
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,"小明", "男");
a.name = "候六";
}
return System.currentTimeMillis()-startTime;
}
}
输出结论
反射10次
- reflectMethodCostTime: 0 ms
- normalMethodCostTime: 0 ms
- reflectFieldCostTime: 0 ms
- normalFieldCostTime: 0 ms
反射100次
- reflectMethodCostTime: 0 ms
- normalMethodCostTime: 0 ms
- reflectFieldCostTime: 0 ms
- normalFieldCostTime: 0 ms
反射1000次
- reflectMethodCostTime: 1 ms
- normalMethodCostTime: 0 ms
- reflectFieldCostTime: 1 ms
- normalFieldCostTime: 0 ms
反射10000次
- reflectMethodCostTime: 7 ms
- normalMethodCostTime: 0 ms
- reflectFieldCostTime: 5 ms
- normalFieldCostTime: 0 ms
反射100000次
- reflectMethodCostTime: 23 ms
- normalMethodCostTime: 1 ms
- reflectFieldCostTime: 17 ms
- normalFieldCostTime: 1 ms
反射1000000次
- reflectMethodCostTime: 77 ms
- normalMethodCostTime: 2 ms
- reflectFieldCostTime: 31 ms
- normalFieldCostTime: 2 ms
结论
- 1.反射确实会比正常访问效率低
- 2.反射在100以内,对于性能的影响是可以忽略的,反射100-1000次之间存在一个反射影响效率的阈值
- 3.整体比较来看直接访问属性和直接通过方法访问属性效率较高,其次是反射获取属性,最后是反射获取方法。
边栏推荐
- Parameter sniffing (1/2)
- Embedded background - chip
- 【ORM框架】
- Guide de signature du Code Appx
- Garbage disposal method based on the separation of smart city and storage and living digital home mode
- Postman interface test VII
- Fiddler simulates the interface test
- Why are social portals rarely provided in real estate o2o applications?
- Three years after graduation
- Arcgis操作: 批量修改属性表
猜你喜欢
Deconvolution popular detailed analysis and nn Convtranspose2d important parameter interpretation
【acwing】786. 第k个数
Internship log - day04
Enterprise practice | construction of banking operation and maintenance index system under complex business relations
Applet sliding, clicking and switching simple UI
ES类和对象、原型
AI moves from perception to intelligent cognition
[original] what is the core of programmer team management?
The method of word automatically generating directory
Google colab loads Google drive (Google drive is used in Google colab)
随机推荐
VS Code指定扩展安装位置
Memory ==c language 1
Why does the starting service report an error when installing MySQL? (operating system Windows)
Download Text, pictures and ab packages used by unitywebrequest Foundation
Postman interface test III
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
Before joining the chain home, I made a competitive product analysis for myself
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
The Himalaya web version will pop up after each pause. It is recommended to download the client solution
Guys, how can mysql-cdc convert the upsert message to append only
STM32中AHB总线_APB2总线_APB1总线这些是什么
Writing file types generated by C language
ES6中的函數進階學習
Applet sliding, clicking and switching simple UI
The physical meaning of imaginary number J
Differences between MCU and MPU
MCU与MPU的区别
Programming features of ISP, IAP, ICP, JTAG and SWD
Programming features of ISP, IAP, ICP, JTAG and SWD
Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is