Come out with a suffix on the method 2

Introduction

This article is a sequel to Come on with the method suffixed. Please note that there are some parts that are slightly broken.

What I was interested in

Last article was just stress relief, but I was worried about performance. I want to know how slow access to the reflection API is.

Apparently with this

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.

at the end

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

Come out with a suffix on the method
Come out with a suffix on the method 2
[Ruby] Cut out a string using the slice method
I immediately stumbled on the standard input method with AtCoder.
Simulate the simplex method with GUI
Programming with ruby (on the way)
Logic to draw a circle with ASCII art on the console
Connecting to a database with Java (Part 1) Maybe the basic method
My thoughts on the equals method (Java)
Declare a method that has a Java return value with the return value data type
Create a jar file with the command
Invoke the character string passed as an argument as a method with send
A note on the libGDX Utils class
Run a DMN with the Camunda DMN Engine
Use a named format with Ruby's format method
A rudimentary note on the Fibonacci sequence
[Rails] When transitioning to a page with link_to, move to the specified location on the page
Hello World, a cross-platform GUI app with Groovy running on the Java platform
A quick note on using jshell with the official Docker image of the JDK
Using templates on the classpath with Apache Velocity
Publish the app made with ruby on rails
Matches annotations on the interface with Spring AOP
Create a multi-key map with the standard library
[Ruby] Exclude duplicate elements with the uniq method.
Determine the current page with Ruby on Rails
Finding pi with the Monte Carlo method? (Ruby)
I made a portfolio with Ruby On Rails
About the method
Proceed with the official Rust documentation on a Docker container (2. Program a number guessing game)
[Java] Cut out a part of the character string with Matcher and regular expression
Call a method with a Kotlin callback block from Java
The story of making a reverse proxy with ProxyServlet
Create a user with an empty password on CentOS7
Display the list in setDetails on the screen with spring-security
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
Challenge the settings for developing with vue.js on Rails 6
Why implement with a singleton instead of a static method
Implemented a strong API for "I want to display ~~ on the screen" with simple CQRS
How to reduce the load on the program even a little when combining characters with JAVA
[Rails] What to do when the view collapses when a message is displayed with the errors method