{"id":1457,"date":"2018-08-15T14:56:33","date_gmt":"2018-08-15T06:56:33","guid":{"rendered":"https:\/\/www.xiaobo.li\/?p=1457"},"modified":"2018-08-15T14:56:33","modified_gmt":"2018-08-15T06:56:33","slug":"%e6%96%87%e6%91%98%ef%bc%9ajava%e5%90%84%e7%a7%8d%e5%8f%8d%e5%b0%84%e6%80%a7%e8%83%bd%e5%af%b9%e6%af%94","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/1457","title":{"rendered":"\u6587\u6458\uff1aJava\u5404\u79cd\u53cd\u5c04\u6027\u80fd\u5bf9\u6bd4"},"content":{"rendered":"<div id=\"main\">\n<div id=\"post_detail\">\n<div class=\"post\">\n<h2>Java\u5404\u79cd\u53cd\u5c04\u6027\u80fd\u5bf9\u6bd4<\/h2>\n<div id=\"cnblogs_post_body\" class=\"blogpost-body\">\n<p>\u5bf9\u5404\u79cd\u65b9\u6cd5\u5b9e\u73b0get\u65b9\u6cd5\u7684\u6027\u80fd\u8fdb\u884c\u4e86\u4e00\u4e2a\u6d4b\u8bd5\u3002<\/p>\n<p>\u603b\u5171\u67095\u4e2a\u6d4b\u8bd5\uff0c\uff0c\u6bcf\u4e2a\u6d4b\u8bd5\u90fd\u662f\u6267\u884c1\u4ebf\u6b21<\/p>\n<p>1. \u76f4\u63a5\u901a\u8fc7Java\u7684get\u65b9\u6cd5<\/p>\n<p>2.\u901a\u8fc7\u9ad8\u6027\u80fd\u7684ReflectAsm\u5e93\u8fdb\u884c\u6d4b\u8bd5<\/p>\n<p>3.\u901a\u8fc7Java Class\u7c7b\u81ea\u5e26\u7684\u53cd\u5c04\u83b7\u5f97Method\u6d4b\u8bd5<\/p>\n<p>4.\u4f7f\u7528Java\u81ea\u5e26\u7684Property\u7c7b\u83b7\u53d6Method\u6d4b\u8bd5<\/p>\n<p>5.BeanUtils\u7684getProperty\u6d4b\u8bd5<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2>\f1 \u6d4b\u8bd5\u7528Bean\u7c7b<\/h2>\n<p>\u6d4b\u8bd5\u5b9a\u4e49\u4e86\u5982\u4e0b\u4e00\u4e2abean\u7c7b\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"java\">public class SimpleBean {\r\n    private String name;\r\n    public String getName() {\r\n        return name;\r\n    }\r\n    public SimpleBean setName(String name) {\r\n        this.name = name;\r\n    }\r\n}<\/code><\/pre>\n<p>\u6ce8\u610f\u5b9a\u4e49\u8981\u4e25\u683c\u9075\u5b88JavaBean\u89c4\u8303\uff0c\u5426\u5219\u5728\u4f7f\u7528\u548c\u53cd\u5c04\u76f8\u5173\u5de5\u5177\u65f6\u4f1a\u51fa\u73b0NoSuchMethodException\u5f02\u5e38\uff0c\u6216\u8005\u5bfc\u81f4\u6027\u80fd\u975e\u5e38\u5dee\uff0cJavaBean\u89c4\u8303\u4e2d\u6700\u91cd\u8981\u7684\u51e0\u70b9\u5982\u4e0b\uff1a<\/p>\n<pre>1.\u7c7b\u5fc5\u987b\u662fpublic, \u62e5\u6709public\u65e0\u53c2\u6784\u9020\u5668,\u8fd9\u6837\u80fd\u591f\u901a\u8fc7\u53cd\u5c04newInstance()\u52a8\u6001\u6784\u5efa\u5bf9\u8c61.\r\n         String className = ...;\r\n         Class beanClass = Class.forName(className);\r\n         Object beanInstance = beanClass.newInstance();\r\n2.\u56e0\u4e3a\u53cd\u5c04newInstance\u4f7f\u7528\u7684\u662f\u65e0\u53c2\u6784\u9020\u5668, \u6240\u4ee5\u5bf9\u8c61\u5b9e\u4f8b\u5316\u548c\u914d\u7f6e\u662f\u5206\u5f00\u7684\r\n3.\u6bcf\u4e00\u4e2aproperty\u90fd\u6709\u4e00\u4e2apublic\u7684getter\u548csetter\u65b9\u6cd5, \u547d\u540d\u65b9\u5f0f\u662fget\/set+\u9996\u5b57\u6bcd\u5927\u5199\u7684property\u540d<\/pre>\n<p>\u7ecf\u6d4b\u8bd5\u5728SimpleBean\u4e3apublic\u65f6\uff0c1\u4ebf\u6b21\u8c03\u7528method.invoke\u65b9\u6cd5\uff1a<\/p>\n<p>javaReflectGet 100000000 times using 218 ms<\/p>\n<p>\u800cSimpleBean\u4e3a\u9ed8\u8ba4\u5305\u53ef\u89c1\u65f6\uff0c1\u4e00\u4ebf\u6b21\u8c03\u7528method.invoke\u65b9\u6cd5\uff1a<\/p>\n<p>javaReflectGet 100000000 times using 12955 ms<\/p>\n<p>&nbsp;<\/p>\n<h2>2.\u6d4b\u8bd5\u4ee3\u7801<\/h2>\n<pre class=\"pure-highlightjs\"><code class=\"java\">public class TestIterator {\r\n    private long times = 100_000_000L;\r\n    private SimpleBean bean;\r\n    private String formatter = \"%s %d times using %d ms\";\r\n    @Before\r\n    public void setUp() throws Exception {\r\n        bean = new SimpleBean();\r\n        bean.setName(\"haoyifen\");\r\n    }\r\n    \/\/\u76f4\u63a5\u901a\u8fc7Java\u7684get\u65b9\u6cd5\r\n    @Test\r\n    public void directGet() {\r\n        Stopwatch watch = Stopwatch.createStarted();\r\n        for (long i = 0; i &lt; times; i++) {\r\n            bean.getName();\r\n        }\r\n        watch.stop();\r\n        String result = String.format(formatter, \"directGet\", times, watch.elapsed(TimeUnit.MILLISECONDS));\r\n        System.out.println(result);\r\n    }\r\n    \/\/\u901a\u8fc7\u9ad8\u6027\u80fd\u7684ReflectAsm\u5e93\u8fdb\u884c\u6d4b\u8bd5\uff0c\u4ec5\u8fdb\u884c\u4e00\u6b21methodAccess\u83b7\u53d6\r\n    @Test\r\n    public void reflectAsmGet() {\r\n        MethodAccess methodAccess = MethodAccess.get(SimpleBean.class);\r\n        Stopwatch watch = Stopwatch.createStarted();\r\n        for (long i = 0; i &lt; times; i++) {\r\n            methodAccess.invoke(bean, \"getName\");\r\n        }\r\n        watch.stop();\r\n        String result = String.format(formatter, \"reflectAsmGet\", times, watch.elapsed(TimeUnit.MILLISECONDS));\r\n        System.out.println(result);\r\n    }\r\n    \/\/\u901a\u8fc7Java Class\u7c7b\u81ea\u5e26\u7684\u53cd\u5c04\u83b7\u5f97Method\u6d4b\u8bd5\uff0c\u4ec5\u8fdb\u884c\u4e00\u6b21method\u83b7\u53d6\r\n    @Test\r\n    public void javaReflectGet() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {\r\n        Method getName = SimpleBean.class.getMethod(\"getName\");\r\n        Stopwatch watch = Stopwatch.createStarted();\r\n        for (long i = 0; i &lt; times; i++) {\r\n            getName.invoke(bean);\r\n        }\r\n        watch.stop();\r\n        String result = String.format(formatter, \"javaReflectGet\", times, watch.elapsed(TimeUnit.MILLISECONDS));\r\n        System.out.println(result);\r\n    }\r\n    \/\/\u4f7f\u7528Java\u81ea\u5e26\u7684Property\u5c5e\u6027\u83b7\u53d6Method\u6d4b\u8bd5\uff0c\u4ec5\u8fdb\u884c\u4e00\u6b21method\u83b7\u53d6\r\n    @Test\r\n    public void propertyGet() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, IntrospectionException {\r\n        Method method = null;\r\n        BeanInfo beanInfo = Introspector.getBeanInfo(SimpleBean.class);\r\n        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();\r\n        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {\r\n            if (propertyDescriptor.getName().equals(\"name\")) {\r\n                method = propertyDescriptor.getReadMethod();\r\n                break;\r\n            }\r\n        }\r\n        Stopwatch watch = Stopwatch.createStarted();\r\n        for (long i = 0; i &lt; times; i++) {\r\n           \r\n            method.invoke(bean);\r\n        }\r\n        watch.stop();\r\n        String result = String.format(formatter, \"propertyGet\", times, watch.elapsed(TimeUnit.MILLISECONDS));\r\n        System.out.println(result);\r\n    }\r\n    \/\/BeanUtils\u7684getProperty\u6d4b\u8bd5\r\n    @Test\r\n    public void beanUtilsGet() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {\r\n        Stopwatch watch = Stopwatch.createStarted();\r\n        for (long i = 0; i &lt; times; i++) {\r\n            BeanUtils.getProperty(bean, \"name\");\r\n        }\r\n        watch.stop();\r\n        String result = String.format(formatter, \"beanUtilsGet\", times, watch.elapsed(TimeUnit.MILLISECONDS));\r\n        System.out.println(result);\r\n    }\r\n}<\/code><\/pre>\n<h2>3.\u6d4b\u8bd5\u7ed3\u679c<\/h2>\n<p>\u57284\u6838i5-4590@3.30GHz\u673a\u5668\u4e0a\u8dd1\u4ee5\u4e0a\u6d4b\u8bd5\uff0c\u7ecf\u8fc7\u591a\u6b21\u6d4b\u91cf\uff0c\u57fa\u672c\u5728\u4ee5\u4e0b\u6570\u503c\u8303\u56f4\u9644\u8fd1\uff0c\u6d4b\u8bd5\u6570\u636e\u5982\u4e0b\uff1a<\/p>\n<p>1. directGet 100000000 times using\u00a0<strong>37<\/strong>\u00a0ms<\/p>\n<p>2. reflectAsmGet 100000000 times using\u00a0<strong>39<\/strong>\u00a0ms<\/p>\n<p>3. javaReflectGet 100000000 times using\u00a0<strong>222<\/strong>\u00a0ms<\/p>\n<p>4. propertyGet 100000000 times using\u00a0<strong>335<\/strong>\u00a0ms<\/p>\n<p>5. beanUtilsGet 100000000 times using\u00a0<strong>20066<\/strong>\u00a0ms<\/p>\n<h2><strong>4.\u7ed3\u679c\u5206\u6790<\/strong><\/h2>\n<p>1.\u4f7f\u7528reflectAsm\u5e93\u7684\u6027\u80fd\u80fd\u548c\u76f4\u63a5\u8c03\u7528get\u65b9\u6cd5\u6301\u5e73<\/p>\n<p>2.Java\u81ea\u5e26\u7684\u53cd\u5c04\u6027\u80fd\u5927\u81f4\u4e3a\u76f4\u63a5get\u76841\/6\u548c1\/9.<\/p>\n<p>3.BeanUtils\u7684getProperty\u975e\u5e38\u7684\u6162\uff0c\u4e3a\u76f4\u63a5get\u6027\u80fd\u76841\/500\uff0c\u4e3aJava\u81ea\u5e26\u53cd\u5c04\u6027\u80fd\u76841\/100\u548c1\/60.<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e3a\u4ec0\u4e48BeanUtils\u7684getProperty\u65b9\u6cd5\u6027\u80fd\u8fd9\u4e48\u6162\uff1f<\/p>\n<p>...<\/p>\n<p><strong>\u6458\u81ea\uff1a<\/strong>https:\/\/www.cnblogs.com\/Frank-Hao\/p\/5839096.html<\/p>\n<p><strong>\u53c2\u8003\uff1a<\/strong><\/p>\n<p>https:\/\/blog.csdn.net\/cike110120\/article\/details\/51163668<\/p>\n<p>https:\/\/blog.csdn.net\/leixingbang1989\/article\/details\/52035336<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u5404\u79cd\u53cd\u5c04\u6027\u80fd\u5bf9\u6bd4 \u5bf9\u5404\u79cd\u65b9\u6cd5\u5b9e\u73b0get\u65b9\u6cd5\u7684\u6027\u80fd\u8fdb\u884c\u4e86\u4e00\u4e2a\u6d4b\u8bd5\u3002 \u603b\u5171\u6709 &hellip; <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/1457\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[297],"tags":[],"class_list":["post-1457","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/1457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=1457"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/1457\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=1457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=1457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=1457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}