{"id":3873,"date":"2019-02-15T15:45:06","date_gmt":"2019-02-15T07:45:06","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3873"},"modified":"2019-02-15T15:45:06","modified_gmt":"2019-02-15T07:45:06","slug":"%e6%af%81%e4%b8%89%e8%a7%82%e7%9a%84-java-for-%e5%be%aa%e7%8e%af%e8%af%ad%e5%8f%a5%e4%bc%98%e5%8c%96%ef%bc%81","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3873","title":{"rendered":"\u6bc1\u4e09\u89c2\u7684 Java for \u5faa\u73af\u8bed\u53e5\u4f18\u5316\uff01"},"content":{"rendered":"<p>\n\t\u636e\u8bf4\uff0c\u8fd9\u662f\u4e00\u9053\u8c37\u6b4c\u7684\u9762\u8bd5\u9898\u3002\n<\/p>\n<p>\n\t\u5c31\u662f\u6709 3 \u4e2a for \u5faa\u73af\uff0c\u6709\u7684\u7a0b\u5e8f\u5458\u4f1a\u628a 3 \u4e2a\u4e00\u6837\u7684 for \u5faa\u73af\u5408\u6210\u4e00\u4e2a\u6765\u5199\uff0c\u6709\u7684\u628a\u4e00\u4e2a for \u5faa\u73af\uff0c\u626f\u6210 3 \u4e2a\u6765\u5199\u3002\u6bd4\u5982\u4e0b\u9762\u7684\u8fd9\u9053\u9898\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\n\/\/\u7b2c\u4e00\u79cd\u60c5\u51b5\r\nfor(int i=0;i&lt;100;i++){\r\n    \/\/opr 1\r\n}\r\nfor(int i=0;i&lt;100;i++){\r\n    \/\/opr 2\r\n}\r\nfor(int i=0;i&lt;100;i++){\r\n    \/\/opr 3\r\n}\r\n\/\/\u7b2c\u4e8c\u79cd\u60c5\u51b5\r\nfor(int i=0;i&lt;100;i++){\r\n    \/\/opr 1\r\n    \/\/opr 2\r\n    \/\/opr 3\r\n}<\/pre>\n<p>\n\t\u90a3\u4e48\u4ece\u6548\u7387\u7b49\u65b9\u9762\u6765\u8bf4\uff0c\u4e0a\u8ff0\u4e24\u79cd Java for \u5faa\u73af\u6709\u533a\u522b\u5417?\n<\/p>\n<p>\n\t\u6211\u76f8\u4fe1\u5927\u90e8\u5206\u4eba\u90fd\u8ba4\u540c\uff0c\u7b2c\u4e8c\u79cd\u60c5\u51b5\u6548\u7387\u66f4\u9ad8\u3002\u4f46\u5b9e\u9645\u662f\u8fd9\u6837\u5417\uff1f\u6211\u4eec\u6765\u5b9e\u9a8c\u4e00\u4e0b\uff01\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u91cd\u65b0\u8ba4\u8bc6 Java \u4e2d\u7684 for \u5faa\u73af\u8bed\u53e5\uff01\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g075fmn2ofj20a108874t.jpg\" style=\"width: 361px;height: 296px\" \/>\n<\/p>\n<p>\n\t\u5148\u8bf4\u7ed3\u8bba\uff1a<strong><span style=\"color:#0000FF\">\u5355\u4e2a\u591a\u64cd\u4f5c\u7684\u5faa\u73af\u6027\u80fd\u548c\u591a\u4e2a\u5355\u64cd\u4f5c\u5faa\u73af\u5dee\u4e0d\u591a\u3002\u5927\u591a\u6570\u60c5\u51b5\u524d\u8005\u4f1a\u597d\u4e00\u70b9\u70b9\u3002\u4e0d\u8fc7\u53ef\u4ee5\u5ffd\u7565\u3002<\/span><\/strong>\n<\/p>\n<p>\n\t\u73b0\u5728\u6211\u4eec\u6765\u770b\u6211\u4eec\u7684\u5b9e\u9a8c\u3002\u5206\u4e09\u79cd\u60c5\u51b5\u3002\n<\/p>\n<p>\n\t\u7b2c\u4e00\u79cd\uff0c\u5faa\u73af\u91cc\u76f4\u63a5\u6267\u884c\u7b80\u5355\u64cd\u4f5c\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\npublic static long oneLoop(int times) {\r\n    long start = System.nanoTime();\r\n    for (int i = 0, x = 0; i &lt; times; i++) {\r\n        x++; x++; x++;\r\n    }\r\n    long end = System.nanoTime();\r\n    return end - start;\r\n}\r\npublic static long threeLoop(int times) {\r\n    long start = System.nanoTime();\r\n    for (int i = 0, x = 0; i &lt; times; i++) {\r\n        x++;\r\n    }\r\n    for (int i = 0, x = 0; i &lt; times; i++) {\r\n        x++;\r\n    }\r\n    for (int i = 0, x = 0; i &lt; times; i++) {\r\n        x++;\r\n    }\r\n    long end = System.nanoTime();\r\n    return end - start;\r\n}<\/pre>\n<p>\n\t\u7b2c\u4e8c\u79cd\uff0c\u5faa\u73af\u91cc\u8c03\u7528\u7b80\u5355\u65b9\u6cd5\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nprivate static int plusOne(int i) {\r\n    return ++i;\r\n}\r\npublic static long oneLoopCallMethod(int times) {\r\n    long start = System.nanoTime();\r\n    for (int i = 0, x = 0; i &lt; times; i++) {\r\n        x = plusOne(x);\r\n        x = plusOne(x);\r\n        x = plusOne(x);\r\n    }\r\n    long end = System.nanoTime();\r\n    return end - start;\r\n}\r\npublic static long threeLoopCallMethod(int times) {\r\n    long start = System.nanoTime();\r\n    for (int i = 0, x = 0; i &lt; times; i++) {\r\n        x = plusOne(x);\r\n    }\r\n    for (int i = 0, x = 0; i &lt; times; i++) {\r\n        x = plusOne(x);\r\n    }\r\n    for (int i = 0, x = 0; i &lt; times; i++) {\r\n        x = plusOne(x);\r\n    }\r\n    long end = System.nanoTime();\r\n    return end - start;\r\n}<\/pre>\n<p>\n\t\u7b2c\u4e09\u79cd\uff0c\u5faa\u73af\u91cc\u6a21\u62df\u6bd4\u8f83\u590d\u6742\u7684\u8c03\u7528\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nprivate static final Random R = new Random();\r\nprivate static final char[] LETTERS = &quot;abcdefghijklmnopqrstuvwxyz&quot;.toCharArray();\r\n\/\/ \u65b9\u6cd51\r\nprivate static String randomWord() {\r\n    int length = R.nextInt(10);\r\n    char[] chars = new char[length];\r\n    for (int i = 0; i &lt; length; i++) {\r\n        chars[i] = LETTERS[R.nextInt(LETTERS.length)];\r\n    }\r\n    return new String(chars);\r\n}\r\n\/\/ \u65b9\u6cd52\r\nprivate static String randomUpperWord() {\r\n    return randomWord().toUpperCase();\r\n}\r\n\/\/ \u65b9\u6cd53\r\nprivate static int wordLength() {\r\n    return randomWord().length();\r\n}\r\n\/\/ \u6d4b\u8bd5\r\npublic static long oneLoopThreeOperation(int times) {\r\n    long start = System.nanoTime();\r\n    for (int i = 0; i &lt; times; i++) {\r\n        randomWord();\r\n        randomUpperWord();\r\n        wordLength();\r\n    }\r\n    long end = System.nanoTime();\r\n    return end - start;\r\n}\r\n\/\/ \u5bf9\u5e94\u6d4b\u8bd5\u62c6\u5206\u62103\u4e2afor\u5faa\u73af\r\npublic static long threeLoopThreeOperation(int times) {\r\n    long start = System.nanoTime();\r\n    for (int i = 0; i &lt; times; i++) {\r\n        randomWord();\r\n    }\r\n    for (int i = 0; i &lt; times; i++) {\r\n        randomUpperWord();\r\n    }\r\n    for (int i = 0; i &lt; times; i++) {\r\n        wordLength();\r\n    }\r\n    long end = System.nanoTime();\r\n    return end - start;\r\n}<\/pre>\n<p>\n\t\u6d4b\u8bd5\u6846\u67b6\u5f88\u7b80\u5355\uff0c\u5355\u5143\u6d4b\u8bd5\u5185\u5faa\u73af 10000 \u6b21\uff0c\u6d4b\u603b\u65f6\u95f4\uff08\u7eb3\u79d2\uff09\u3002\u7136\u540e\u6bcf\u4e2a\u5355\u5143\u6d4b\u8bd5\u91cd\u590d100\u6b21\uff0c\u4e22\u5f03\u524d 10 \u6b21\u9884\u70ed\u7ed3\u679c\uff0c\u8ba1\u5e73\u5747\u65f6\u95f4\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nlong[] result = new long[6];\r\nint loops= 100;\r\nfor (int i = 0; i &lt; loops; i++) {\r\n    if (i &gt;= 10) { \/\/ \u4e22\u5f03\u524d10\u6b21\u9884\u70ed\r\n        result[0] += oneLoop(10000);\r\n        result[1] += threeLoop(10000);\r\n        result[2] += oneLoopCallMethod(10000);\r\n        result[3] += threeLoopCallMethod(10000);\r\n        result[4] += oneLoopThreeOperation(10000);\r\n        result[5] += threeLoopThreeOperation(10000);\r\n    }\r\n}\r\n\/\/ \u6253\u5370\u4ee5\u4e0a\u65f6\u95f4\/90<\/pre>\n<p>\n\t\u7ed3\u679c\u6709\u70b9\u51fa\u4e4e\u610f\u6599\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u4f60\u771f\u7684\u4f1a for \u5faa\u73af\u5417\uff1f\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g075iqi2nlj20ba037wew.jpg\" style=\"width: 406px;height: 115px\" \/>\n<\/p>\n<ul>\n<li>\n\t\t\u7b80\u5355\u81ea\u589e\u64cd\u4f5c\uff0c\u5355\u4e2afor\u6bd4\u4e09\u4e2afor\u5feb\u4e00\u70b9\u3002\n\t<\/li>\n<li>\n\t\t\u8c03\u7528\u540c\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5355\u4e2afor\u6bd4\u4e09\u4e2afor\u6162\u4e86\u63a5\u8fd1\u4e00\u500d\u3002\n\t<\/li>\n<li>\n\t\t\u63a5\u4e0b\u6765\u590d\u6742\u573a\u666f\u4e0b\u7684\u7ed3\u679c\u6062\u590d\u6b63\u5e38\uff0c\u5355\u4e2a\u5faa\u73af\u8fd8\u662f\u8981\u597d\u4e00\u4e9b\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u4e3a\u4e86\u641e\u6e05\u695a\u4e3a\u4ec0\u4e48\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u4e3a\u4ec0\u4e48\u7528\u4e09\u4e2afor\u5faa\u73af\u53cd\u800c\u5feb\uff0c\u7528 -XX:+PrintCompilation \u53c2\u6570\u6253\u5370\u4e86 TIJ \u7684\u7f16\u8bd1\u60c5\u51b5\u3002\n<\/p>\n<p>\n\t\u771f\u5b9e\u7684\u539f\u56e0\uff0c\u56e0\u4e3a\u7528\u4e00\u4e2a\u5faa\u73af\uff0c\u53ea\u4f1a\u5185\u8054 plusOne() \u65b9\u6cd5\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nfor (int i = 0, x = 0; i &lt; times; i++) {\r\n    x = plusOne(x);\r\n    x = plusOne(x);\r\n    x = plusOne(x);\r\n}<\/pre>\n<p>\n\t\u4f46\u5982\u679c\u662f\u76f8\u540c\u7684\u4e09\u6b21\u5faa\u73af\uff0cTIJ \u6709\u53ef\u80fd\u76f4\u63a5\u628a\u6574\u4e2a\u5faa\u73af\u7f16\u8bd1\u4e86\u3002\u540e\u9762\u4e24\u6b21\u5c31\u4f1a\u8282\u7701\u65f6\u95f4\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nfor (int i = 0, x = 0; i &lt; times; i++) {\r\n    x = plusOne(x);\r\n}\r\nfor (int i = 0, x = 0; i &lt; times; i++) { \/\/ TIJ\u7f16\u8bd1\u4e86\u6574\u4e2a\u5faa\u73af\r\n    x = plusOne(x);\r\n}\r\nfor (int i = 0, x = 0; i &lt; times; i++) {\r\n    x = plusOne(x);\r\n}<\/pre>\n<p>\n\t\u6709\u7f51\u53cb\u5728 github \u4e0a\uff0c\u5bf9\u8fd9\u4e2a\u6d4b\u8bd5\u6539\u6210 C++ \u6269\u5927\u5230\u8dd110\u4ebf(1E9)\u6b21\u8ba1\u6570\u3002\u6211\u60f3\u8fd9\u4e2a\u6d4b\u8bd5\u4ee5\u540e\u53ef\u80fd\u4f1a\u88ab\u5404\u4e2a\u8bed\u8a00\u7ed9\u73a9\u574f\uff01\n<\/p>\n<p>\n\t\u5bf9\u4e8e\u8fd9\u4e2a\u7ed3\u8bba\uff0c\u6709 CPU \u8bbe\u8ba1\u56e2\u961f\u7684\u4eba\u5458\u56de\u590d\u5230\u3002\u9488\u5bf9\u4e8e for \u7684\u6027\u80fd\uff0ccpu \u4f7f\u7528\u7684\u662f branch predictor \u4e2d\u7684 loop \u6a21\u5757\u3002\u8fd9\u4e2a\u6a21\u5757\u4f1a\u6839\u636e\u5386\u53f2\u4fe1\u606f\uff0c\u53bb\u9884\u6d4b for \u5faa\u73af\u672a\u6765\u7684\u884c\u4e3a\u3002\n<\/p>\n<p>\n\t\u5e76\u4ece 3 \u4e2a\u65b9\u9762\u7ed9\u51fa\u4e86\u5206\u6790\uff1a\n<\/p>\n<ul>\n<li>\n\t\t\u6267\u884c\u6027\u80fd\uff1a \u4e00\u4e2afor\u5faa\u73af\u6bd4\u4e09\u4e2afor\u5faa\u73af\uff0ccpu\u5728\u7b2c\u4e00\u6b21\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u5c11\u51fa\u73b0\u4e24\u6b21misp\uff0c\u4f46\u662f\uff0c\u5728\u4ee5\u540e\u518d\u6b21\u9047\u5230\u8fd9\u4e2afor\u8bed\u53e5\u7684\u9884\u6d4b\u8fc7\u7a0b\u4e2d\u4e24\u79cd\u5199\u6cd5\u662f\u4e00\u6837\u7684\u3002\u603b\u7ed3\uff1a\u4e00\u4e2afor\u5faa\u73af\u4f1a\u8282\u7701\u51e0\u5341\u4e2a\u65f6\u949f\u5468\u671f\uff0c\u5177\u4f53\u6570\u5b57\u548ccpu\u67b6\u6784\u548c\u82af\u7247\u9891\u7387\u6709\u5173\uff0c\u5927\u7ea65ns\u5de6\u53f3\u3002\n\t<\/li>\n<li>\n\t\t\u5360\u7528CPU\u8d44\u6e90\uff1a\u4e00\u4e2afor\u5faa\u73af\u4f1a\u6bd4\u4e09\u4e2afor\u5faa\u73af\u66f4\u5c11\u3002\u8fd9\u4e2a\u8d44\u6e90\u53ea\u662f\u9884\u6d4b\u4e2d\u4e00\u4e2a\u5b50\u6a21\u5757\u7684\u8d44\u6e90\uff0c\u5e76\u4e0d\u662f\u6267\u884c\u5355\u5143\u7684\u8d44\u6e90\u3002\u867d\u7136\uff0c\u8282\u7701\u4e09\u5206\u4e4b\u4e00\uff0c\u4f46\u662f\uff0c\u5e76\u4e0d\u662f\u5f88\u591a\u3002\n\t<\/li>\n<li>\n\t\tJava\u4ee3\u7801\u53ef\u8bfb\u6027\uff1a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5bf9\u4e8e\u8f6f\u4ef6\u5de5\u7a0b\u5e08\u5f88\u91cd\u8981\u7684\u3002\u4ee5\u4e0a\u4e09\u4e2afor\u5faa\u73af\uff0c\u6240\u591a\u5360\u7528\u7684CPU\u8d44\u6e90\u548c\u803d\u8bef\u7684\u65f6\u95f4\uff0c\u5bf9\u4e8e\u6574\u4e2a\u7a0b\u5e8f\u6765\u8bb2\uff0c\u5b9e\u5728\u5fae\u4e0d\u8db3\u9053\u3002\u5bf9\u4e8e\u521d\u5b66\u8005\uff0c\u5199\u51fa\u6574\u6d01\u7684\u4ee3\u7801\u662f\u5341\u5206\u91cd\u8981\u7684\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u4ee5\u4e0a\u662f\u4e0d\u662f\u51fa\u4e4e\u4f60\u4eec\u7684\u610f\u6599\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3873\">\u6bc1\u4e09\u89c2\u7684 Java for \u5faa\u73af\u8bed\u53e5\u4f18\u5316\uff01<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u636e\u8bf4\uff0c\u8fd9\u662f\u4e00\u9053\u8c37\u6b4c\u7684\u9762\u8bd5\u9898\u3002 \u5c31\u662f\u6709 3 \u4e2a for \u5faa\u73af\uff0c\u6709\u7684\u7a0b\u5e8f\u5458\u4f1a\u628a 3 \u4e2a\u4e00\u6837\u7684 for \u5faa\u73af\u5408\u6210\u4e00\u4e2a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[69,567,618,337,634],"class_list":["post-3873","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-java-","tag-java8","tag-javascriptapi"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3873"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3873\/revisions"}],"predecessor-version":[{"id":3874,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3873\/revisions\/3874"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}