{"id":202798,"date":"2025-05-22T09:05:47","date_gmt":"2025-05-22T01:05:47","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202798\/"},"modified":"2025-05-22T09:05:47","modified_gmt":"2025-05-22T01:05:47","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8%e4%b8%8d%e5%90%8c%e7%9a%84-java-%e7%89%88%e6%9c%ac%e4%b9%8b%e9%97%b4%e6%af%94%e8%be%83%e5%87%bd%e6%95%b0%e6%89%a7%e8%a1%8c%e6%95%88%e7%8e%87%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202798\/","title":{"rendered":"\u5982\u4f55\u5728\u4e0d\u540c\u7684 Java \u7248\u672c\u4e4b\u95f4\u6bd4\u8f83\u51fd\u6570\u6267\u884c\u6548\u7387\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5728\u4e0d\u540c\u7684 Java \u7248\u672c\u4e4b\u95f4\u6bd4\u8f83\u51fd\u6570\u6267\u884c\u6548\u7387\uff1f<\/h1>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728\u4e0d\u540c\u7684 Java \u7248\u672c\u4e4b\u95f4\u6bd4\u8f83\u51fd\u6570\u6267\u884c\u6548\u7387\uff1f\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u6216\u8005\u662f\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 Java \u5fae\u57fa\u51c6\u6d4b\u8bd5 (JMH) \u6846\u67b6\uff0c\u53ef\u4ee5\u6bd4\u8f83 Java \u7248\u672c\u4e2d\u51fd\u6570\u7684\u6267\u884c\u6548\u7387\u3002\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u3001\u521b\u5efa\u57fa\u51c6\u6d4b\u8bd5\u7c7b\u3001\u914d\u7f6e\u57fa\u51c6\u6d4b\u8bd5\u548c\u8fd0\u884c\u57fa\u51c6\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u751f\u6210\u62a5\u544a\uff0c\u663e\u793a\u4e0d\u540c Java \u7248\u672c\u4e2d\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\uff0c\u4f8b\u5982 Java 8 \u548c Java 16\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729943134671cd65e2a499.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728\u4e0d\u540c\u7684 Java \u7248\u672c\u4e4b\u95f4\u6bd4\u8f83\u51fd\u6570\u6267\u884c\u6548\u7387\uff1f<\/strong><\/p>\n<p>\u4e86\u89e3\u4e0d\u540c Java \u7248\u672c\u4e2d\u51fd\u6570\u6267\u884c\u6548\u7387\u7684\u5dee\u5f02\u5bf9\u4e8e\u5e94\u7528\u7a0b\u5e8f\u4f18\u5316\u81f3\u5173\u91cd\u8981\u3002<strong>Java \u5fae\u57fa\u51c6\u6d4b\u8bd5 (JMH)<\/strong> \u662f\u4e00\u4e2a\u6d41\u884c\u7684\u6846\u67b6\uff0c\u7528\u4e8e\u5bf9 Java \u4ee3\u7801\u8fdb\u884c\u5fae\u57fa\u51c6\u6d4b\u8bd5\u548c\u6bd4\u8f83\u3002<\/p>\n<p><strong>1. \u8bbe\u7f6e\u73af\u5883<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u5728\u9879\u76ee\u4e2d\u6dfb\u52a0 JMH \u4f9d\u8d56\u9879\uff1a<\/p>\n<pre>&lt;dependency&gt;\n    &lt;groupId&gt;org.openjdk.jmh&lt;\/groupId&gt;\n    &lt;artifactId&gt;jmh-core&lt;\/artifactId&gt;\n    &lt;version&gt;1.36&lt;\/version&gt;\n&lt;\/dependency&gt;<\/pre>\n<p><strong>2. \u521b\u5efa\u57fa\u51c6\u6d4b\u8bd5\u7c7b<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a Java \u7c7b\u6765\u5305\u542b\u8981\u6bd4\u8f83\u7684\u57fa\u51c6\u6d4b\u8bd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>import org.openjdk.jmh.annotations.*;\n\n@State(Scope.Thread)\npublic class FunctionEfficiencyBenchmark {\n\n    \/\/ \u51fd\u6570\u8981\u6267\u884c\u7684\u6b21\u6570\n    @Param({\"1000\", \"10000\", \"100000\"})\n    public int iterations;\n\n    public long sum(int[] arr) {\n        long sum = 0;\n        for (int i : arr) {\n            sum += i;\n        }\n        return sum;\n    }\n}<\/pre>\n<p><strong>3. \u914d\u7f6e\u57fa\u51c6\u6d4b\u8bd5<\/strong><\/p>\n<p>\u4f7f\u7528 <code>@Benchmark<\/code> \u6ce8\u89e3\u6765\u6807\u6ce8\u8981\u6d4b\u91cf\u7684\u51fd\u6570\uff0c\u5e76\u4f7f\u7528 <code>@Warmup<\/code> \u548c <code>@Measurement<\/code> \u6ce8\u89e3\u6765\u914d\u7f6e\u57fa\u51c6\u6d4b\u8bd5\u8fd0\u884c\u3002<\/p>\n<pre>@Benchmark\n@Warmup(iterations = 10)\n@Measurement(iterations = 10)\npublic void benchmarkSum() {\n    int[] arr = new int[iterations];\n    for (int i = 0; i &lt; iterations; i++) {\n        arr[i] = i;\n    }\n    sum(arr);\n}<\/pre>\n<p><strong>4. \u8fd0\u884c\u57fa\u51c6\u6d4b\u8bd5<\/strong><\/p>\n<p>\u8fd0\u884c\u57fa\u51c6\u6d4b\u8bd5\u7c7b\u4ee5\u751f\u6210\u62a5\u544a\uff1a<\/p>\n<pre>mvn clean install\njava -jar target\/benchmarks.jar<\/pre>\n<p><strong>5. \u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4e0b\u8868\u663e\u793a\u4e86 Java 8 \u548c Java 16 \u4e2d <code>sum<\/code> \u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>Java \u7248\u672c<\/th>\n<th>1000 \u6b21<\/th>\n<th>10000 \u6b21<\/th>\n<th>100000 \u6b21<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Java 8<\/td>\n<td>0.011 ms<\/td>\n<td>0.092 ms<\/td>\n<td>1.133 ms<\/td>\n<\/tr>\n<tr>\n<td>Java 16<\/td>\n<td>0.006 ms<\/td>\n<td>0.054 ms<\/td>\n<td>0.654 ms<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7531\u6b64\u53ef\u89c1\uff0cJava 16 \u4e2d <code>sum<\/code> \u51fd\u6570\u7684\u6267\u884c\u6548\u7387\u6bd4 Java 8 \u9ad8\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5728\u4e0d\u540c\u7684 Java \u7248\u672c\u4e4b\u95f4\u6bd4\u8f83\u51fd\u6570\u6267\u884c\u6548\u7387\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728\u4e0d\u540c\u7684 Java \u7248\u672c\u4e4b\u95f4&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202798","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202798","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202798"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202798\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}