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.
** 1. Normal instantiation **
qiita.java
private static Oimo getInstance01() throws Exception {
return new Oimo();
}
** 2. Instantiation using reflection **
qiita.java
private static Oimo getInstance02() throws Exception {
Class<?> clazz = Class.forName("kensho01.Oimo");
return (Oimo) clazz.newInstance();
}
--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.
** 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