Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use reflection to obtain the rate of Class method in Java

Shulou Source: shulou.com Published: 2022-06-02 10:25:25 09月24日 Update

This article is about how to use reflection to obtain class method speed in Java, the editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.

Reflection introduction

Java reflection refers to the ability to check the contents of any object and call any method in the running state of the program. For any object, we can call its methods and properties. We call this function of dynamically obtaining object information and calling object methods as reflection mechanism.

Test code

Reflect the called class

Public class ClassA {private String name; public void setName (String name) {this.name = name;}}

Test main class

Import lombok.extern.slf4j.Slf4j;import java.lang.reflect.Method;@Slf4jpublic class MapMain {private static final int COUNT = 1000000; 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; iTunes +) {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 the class to avoid looking for Class clzA = Class.forName ("com.yq.myreflect.ClassA") multiple times; Class [] argsType = new Class [1] ArgsType [0] = String.class; / / caching method to avoid multiple lookups for Method m = clzA.getMethod ("setName", argsType); for (int I = 0; I < COUNT; iTunes +) {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 results

Average result of multiple runs

Starting... native call:33reflect call:126cache call:16 result analysis

You can see that if we cache the class and the method to be called, using reflection is faster than calling it directly.

/ / cache class to avoid multiple lookups for Class clzA = Class.forName ("com.yq.myreflect.ClassA"); Class [] argsType = new Class [1]; argsType [0] = String.class; / / cache methods to avoid multiple searches on how to use reflection to obtain class method rates in Java. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Method reflection cache object result test rate more knowledge article run utility code information content function dynamic that is properties work will Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia vpn Huawei Shulou Technology MySQL