Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report