[Java] Is reflection really heavy? Performance comparison


I wanted to use reflection when creating the base class, but was it really heavy because I had the information that reflection was heavy? I wanted to know how heavy it was, so I actually measured it.

Verification environment

Source code for verification

** 1. Normal instantiation **


private static Oimo getInstance01() throws Exception {
	return new Oimo();

** 2. Instantiation using reflection **


private static Oimo getInstance02() throws Exception {
	Class<?> clazz = Class.forName("kensho01.Oimo");
	return (Oimo) clazz.newInstance();

Implementation procedure

--Use the verification source code to instantiate 100,000 times, and measure each 3 times to calculate the average time. Round to the first decimal place.

Implementation results

** 1. Normal instantiation **

--First time: 4 ms --Second time: 3 ms --Third time: 3 milliseconds --Average: 3.33 ms

** 2. Instantiation using reflection **

--First time: 63 ms --Second time: 69 ms --Third time: 65 ms --Average: 65.67 ms


It can be seen that it takes an average of 65.67 milliseconds for instantiation using reflection, while it takes an average of 3.33 milliseconds for normal instance generation, which is about 20 times longer. From this, it was found that instantiation using reflection is heavy, and use for the source code of the product should be avoided as much as possible, apart from the test code.

See you again (^_^) Noshi

Recommended Posts

[Java] Is reflection really heavy? Performance comparison
What is java
What is Java <>?
What is Java
[Java] Map comparison
Java framework comparison
The comparison of enums is ==, and equals is good [Java]
Java Performance Chapter 1 Introduction
Java version notation comparison
Java app performance tuning
What is Java Encapsulation?
What is Java technology?
What is Java API-java
[Development] Java framework comparison
[Java] What is flatMap?
Java Performance Chapter 3 Java Performance Toolbox
[Java] What is JavaBeans?
[Java] What is ArrayList?
Is short-circuit evaluation really fast? Difference between && and & in Java