当前位置:网站首页>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 .
边栏推荐
- 网上可以开炒股账户吗安全吗
- Before joining the chain home, I made a competitive product analysis for myself
- STM32中AHB总线_APB2总线_APB1总线这些是什么
- MCU is the most popular science (ten thousand words summary, worth collecting)
- 基于gis三维可视化技术的智慧城市建设
- Internship log - day07
- PDF文档签名指南
- [ORM framework]
- In addition to the objective reasons for overtime, what else is worth thinking about?
- [sword finger offer] 42 Stack push in and pop-up sequence
猜你喜欢

Pytest learning - dayone

Embedded background - chip

搭建物联网硬件通信技术几种方案

ArcGIS operation: converting DWG data to SHP data

Win10安装VS2015

Postman interface test VII

Google colab loads Google drive (Google drive is used in Google colab)

基于gis三维可视化技术的智慧城市建设

The request object parses the request body and request header parameters

LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
随机推荐
ES6中的函数进阶学习
IPv4套接字地址结构
Deconvolution popular detailed analysis and nn Convtranspose2d important parameter interpretation
ORM -- query type, association query
request对象对请求体,请求头参数的解析
STM32 Basics - memory mapping
Integer inversion
ORM -- logical relation and & or; Sort operation, update record operation, delete record operation
web3.0系列之分布式存储IPFS
Postman interface test VI
Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
【acwing】786. Number k
Guid primary key
STM32基础知识—内存映射
ArcGIS operation: converting DWG data to SHP data
Advanced function learning in ES6
Introduction to uboot
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
为什么安装mysql时starting service报错?(操作系统-windows)
MongoDB创建一个隐式数据库用作练习