It suddenly occurred to me today Java reflex . And then we tested it 1.8.0_191 Reflection performance of version , I didn't expect that the performance of reflection is better than that of direct call .

<> Reflection introduction

Java Reflection refers to the running state of a program , Can check the content of any object and call any method . For any object , We can all call its methods and properties . We call this function of dynamically obtaining object information and calling object methods as reflection mechanism .

<> Test code

The class that reflects the call
package com.yq.myreflect; public class ClassA { private String name; public
void setName(String name) { this.name = name; } }
Test main class
package com.yq.myreflect; import lombok.extern.slf4j.Slf4j; import java.lang.
reflect.Method; @Slf4j public class MapMain { private static final int COUNT =
100000; public static void main(String[] args) { System.out.println("starting
..."); long start = System.currentTimeMillis(); for (int i = 0; i < COUNT; i++)
{ ClassA clzA = new ClassA(); clzA.setName("A" + i); } long end = System.
currentTimeMillis(); long diff = end - start; System.out.println("native call:"
+ diff); try { long start2 = System.currentTimeMillis(); for (int i = 0; i <
COUNT; i++) { Class clzA = Class.forName("com.yq.myreflect.ClassA"); Class<?>[]
argsType= new Class[1]; argsType[0] = String.class; Method m = clzA.getMethod(
"setName", argsType); Object obj = clzA.newInstance(); m.invoke(obj, "A" + i); }
long end2 = System.currentTimeMillis(); long diff2 = end2 - start2; System.out.
println("reflect call:" + diff2); } catch (Exception ex) { ex.printStackTrace();
} try { long start3 = System.currentTimeMillis(); // Cache class , Avoid multiple lookups Class clzA =
Class.forName("com.yq.myreflect.ClassA"); Class<?>[] argsType = new Class[1];
argsType[0] = String.class; // Cache method , Avoid multiple lookups Method m = clzA.getMethod("setName",
argsType); for (int i = 0; i < COUNT; i++) { Object obj = clzA.newInstance(); m.
invoke(obj, "A" + i); } long end3 = System.currentTimeMillis(); long diff3 =
end3- start3; System.out.println("cache call:" + diff3); } catch (Exception ex)
{ ex.printStackTrace(); } } }
<> test result

Average results of multiple runs
starting ... native call:33 reflect call:126 cache call:16
<> Result analysis

As you can see, if we cache classes , And the method to call , Using reflection is faster than calling directly .
// Cache class , Avoid multiple lookups Class clzA = Class.forName("com.yq.myreflect.ClassA");
Class<?>[] argsType = new Class[1]; argsType[0] = String.class; // Cache method , Avoid multiple lookups
Method m = clzA.getMethod("setName", argsType);
<> Additional

For netizens , Tested ClassA Too simple , Can it reflect the real situation , I put ClassA Some more attributes have been added , When the mode is called , Some of them are used . The results are similar .

<>ClassA code
public class ClassA { private String name; private int age; private String
address; private Map<String, String> projectIdDetailMap = new HashMap<>();
public void setName(String name) { this.name = name; } public void setAge(
Integer age) { this.age = age; } public void setAddress(String address) { this.
name= address; } public void addProjectExperience(String projectName, String
details) { projectIdDetailMap.put(projectName, details); } }
<> Test code
@Slf4j public class MapMain { private static final int COUNT = 100000; public
static void main(String[] args) { System.out.println("starting ..."); long start
= System.currentTimeMillis(); for (int i = 0; i < COUNT; i++) { ClassA clzA =
new ClassA(); clzA.setName("A" + i); clzA.setAge(i); clzA.setAddress(" Yanta District, Xi'an City ");
clzA.addProjectExperience(" Continuous integration products ", "agent development "); clzA.addProjectExperience(
" Continuous deployment of products ", " Engine development "); clzA.addProjectExperience(" Cloud platform ", " New platform planning main function development "); } long end
= System.currentTimeMillis(); long diff = end - start; System.out.println(
"native call:" + diff); try { long start2 = System.currentTimeMillis(); for (int
i= 0; i < COUNT; i++) { Class clzA = Class.forName("com.yq.myreflect.ClassA");
Class<?>[] argsType = new Class[1]; argsType[0] = String.class; Class<?>[]
argsTypeInt= new Class[1]; argsTypeInt[0] = Integer.class; Class<?>[]
argsTypeMap= new Class[2]; argsTypeMap[0] = String.class; argsTypeMap[1] =
String.class; Method setName = clzA.getMethod("setName", argsType); Method
setAge= clzA.getMethod("setAge", argsTypeInt); Method setAddress = clzA.
getMethod("setAddress", argsType); Method addProjectExperience = clzA.getMethod(
"addProjectExperience", argsTypeMap); Object obj = clzA.newInstance(); setName.
invoke(obj, "A" + i); setAge.invoke(obj, i); setAddress.invoke(obj, " Yanta District, Xi'an City ");
addProjectExperience.invoke(obj, " Continuous integration products ", "agent development "); addProjectExperience.
invoke(obj, " Continuous deployment of products ", " Engine development "); addProjectExperience.invoke(obj, " Cloud platform ",
" New platform planning main function development "); } long end2 = System.currentTimeMillis(); long diff2 = end2 -
start2; System.out.println("reflect call:" + diff2); } catch (Exception ex) { ex
.printStackTrace(); } try { long start3 = System.currentTimeMillis(); Class clzA
= Class.forName("com.yq.myreflect.ClassA"); Class<?>[] argsType = new Class[1];
argsType[0] = String.class; Class<?>[] argsTypeInt = new Class[1]; argsTypeInt[0
] = Integer.class; Class<?>[] argsTypeMap = new Class[2]; argsTypeMap[0] =
String.class; argsTypeMap[1] = String.class; Method setName = clzA.getMethod(
"setName", argsType); Method setAge = clzA.getMethod("setAge", argsTypeInt);
Method setAddress= clzA.getMethod("setAddress", argsType); Method
addProjectExperience= clzA.getMethod("addProjectExperience", argsTypeMap); for (
int i = 0; i < COUNT; i++) { Object obj = clzA.newInstance(); setName.invoke(obj
, "A" + i); setAge.invoke(obj, i); setAddress.invoke(obj, " Yanta District, Xi'an City ");
addProjectExperience.invoke(obj, " Continuous integration products ", "agent development "); addProjectExperience.
invoke(obj, " Continuous deployment of products ", " Engine development "); addProjectExperience.invoke(obj, " Cloud platform ",
" New platform planning main function development "); } long end3 = System.currentTimeMillis(); long diff3 = end3 -
start3; System.out.println("cache call:" + diff3); } catch (Exception ex) { ex.
printStackTrace(); } } }
<> Operation results

my Java8, Windows10 Results of multiple runs above ,

native call:91
reflect call:530
cache call:71

Technology
©2019-2020 Toolsou All rights reserved,
A summary of the computer retest interview questions var a = 0, b = 0; function A(a) {A = function (b)... An interview question about scope 【 Data structure and algorithm 1】 Sparse array 2020—— Net tripod cup part writeupPTA-MOOC《Python Programming Zhejiang University 》 Puzzle A Chapter 2 programming problems python closure uniapp Dynamic routing parameter transfer ( Delivery object ) How to use it Python Batch import Excel Combined use Pandas integration Lambda Expression and functional interface 【Java Details of knowledge points 3】 Serializable and Deserialize