This article is a sequel to Come on with the method suffixed. Please note that there are some parts that are slightly broken.
Last article was just stress relief, but I was worried about performance. I want to know how slow access to the reflection API is.
For the time being, I let him vomit for a while.
suffixMethodsCaller.java
package suffixMethodsCall;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class suffixMethodsCaller {
/**
* <pre>
*Execute all getters with suffix included in suffixMethods,
*Verify that the matchTargetCode at the beginning is included.
* </pre>
*/
public static void main(String[] args) {
//Measurement start
long measureStart = System.currentTimeMillis();
//Matching string
String matchTargetCode = "J";
//Code list obtained from the method with suffix
List<String> codeList = new ArrayList<String>();
//Specify the class and method as a character string
suffixMethods sm = new suffixMethods();
String clazz = sm.getClass().getName();
String baseSuffixMethod = "getReturnCode";
int suffixPartStart = 1;
int suffixPartEnd = 10;
//Execute a method with suffix using reflection
try {
Class<?> c = Class.forName(clazz);
Object myObj = c.newInstance();
//Loop the suffix part and plunge into the code list
for ( ; suffixPartStart <= suffixPartEnd; suffixPartStart++) {
//Execution method settings
Method m = c.getMethod(baseSuffixMethod + String.format("%02d", suffixPartStart));
//Stuff into code list
codeList.add(m.invoke(myObj).toString());
}
} catch(ReflectiveOperationException e) {
e.printStackTrace();
}
//Whether the character string to be matched is included in the execution result list of the method with suffix is output as bool
System.out.println(codeList.contains(matchTargetCode));
//The measurement is over!
long measureEnd = System.currentTimeMillis();
System.out.println((measureEnd - measureStart) + "ms");
}
}
Execution result
true
44ms
I see, i see. Next, let's go crazy.
suffixMethodsCaller2.java
package suffixMethodsCall;
public class suffixMethodsCaller2 {
/**
* <pre>
*Execute all getters with suffix included in suffixMethods,
*Verify that the matchTargetCode at the beginning is included.
* </pre>
*/
public static void main(String[] args) {
//Measurement start
long measureStart = System.currentTimeMillis();
//Matching string
String matchTargetCode = "J";
suffixMethods sm = new suffixMethods();
//Whether the character string to be matched is included in the execution result list of the method with suffix is output as bool
System.out.println(sm.getReturnCode01().equals(matchTargetCode) || sm.getReturnCode02().equals(matchTargetCode)
|| sm.getReturnCode03().equals(matchTargetCode) || sm.getReturnCode04().equals(matchTargetCode)
|| sm.getReturnCode05().equals(matchTargetCode) || sm.getReturnCode06().equals(matchTargetCode)
|| sm.getReturnCode07().equals(matchTargetCode) || sm.getReturnCode08().equals(matchTargetCode)
|| sm.getReturnCode09().equals(matchTargetCode) || sm.getReturnCode10().equals(matchTargetCode));
//The measurement is over!
long measureEnd = System.currentTimeMillis();
System.out.println((measureEnd - measureStart) + "ms");
}
}
Execution result
true
1ms
e. .. .. ?? really? It makes such a difference.
I was a little worried, so another one. It's a bad sentence, but I want to reverse the truth and see if there is any change. I improved the accuracy to nanoTime.
suffixMethodsCaller3.java
package suffixMethodsCall;
public class suffixMethodsCaller3 {
/**
* <pre>
*Execute all getters with suffix included in suffixMethods,
*Verify that the matchTargetCode at the beginning is included.
* </pre>
*/
public static void main(String[] args) {
//Measurement start
long measureStart = System.nanoTime();
//Matching string
String matchTargetCode = "J";
suffixMethods sm = new suffixMethods();
//Whether the character string to be matched is included in the execution result list of the method with suffix is output as bool
System.out.println(!sm.getReturnCode01().equals(matchTargetCode) && !sm.getReturnCode02().equals(matchTargetCode)
&& !sm.getReturnCode03().equals(matchTargetCode) && !sm.getReturnCode04().equals(matchTargetCode)
&& !sm.getReturnCode05().equals(matchTargetCode) && !sm.getReturnCode06().equals(matchTargetCode)
&& !sm.getReturnCode07().equals(matchTargetCode) && !sm.getReturnCode08().equals(matchTargetCode)
&& !sm.getReturnCode09().equals(matchTargetCode) && !sm.getReturnCode10().equals(matchTargetCode));
//The measurement is over!
long measureEnd = System.nanoTime();
System.out.println((measureEnd - measureStart) + "ns");
}
}
It's hard to read.
Execution result
false
1127314ns
Hmm. I will try to make the previous one nano-order.
Execution result
true
1100219ns
Oh. I tried it several times and the result was almost the same. It's just the superiority or inferiority of readability. I learned a lot.
However, I am surprised at the slowness when using reflection. Is it because there is a for statement? The result was the same even if I thought that I lost the loop.
Reflection doesn't seem to be recommended for business applications, I personally like it because it can be used very interestingly for internal Util etc.
This article started with stress relief, but when I actually moved my hands, I learned a lot. I hope it will be helpful to anyone.
Recommended Posts