{"id":10006037,"date":"2022-07-27T23:17:22","date_gmt":"2022-07-27T15:17:22","guid":{"rendered":"https:\/\/ityet.com\/?p=10006037"},"modified":"2022-07-27T23:17:22","modified_gmt":"2022-07-27T15:17:22","slug":"%e5%88%ab%e5%86%8d%e7%94%a8-system-currenttimemillis-%e7%bb%9f%e8%ae%a1%e8%80%97%e6%97%b6%e4%ba%86%ef%bc%8c%e5%a4%aa-low%ef%bc%8c%e8%af%95%e8%af%95stopwatch%e5%90%a7%ef%bc%8c%e5%a4%9f%e4%bc%98","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006037","title":{"rendered":"\u522b\u518d\u7528 System.currentTimeMillis \u7edf\u8ba1\u8017\u65f6\u4e86\uff0c\u592a Low\uff0c\u8bd5\u8bd5StopWatch\u5427\uff0c\u591f\u4f18\u96c5\uff01"},"content":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u4e8c\u54e5\u5440\uff01<\/p>\n<p>\u6628\u5929\uff0c\u4e00\u4f4d\u7403\u53cb\u95ee\u6211\u80fd\u4e0d\u80fd\u7ed9\u4ed6\u89e3\u91ca\u4e00\u4e0b <code>@SpringBootApplication<\/code> \u6ce8\u89e3\u662f\u4ec0\u4e48\u610f\u601d\uff0c\u8fd8\u6709 Spring Boot \u7684\u8fd0\u884c\u539f\u7406\uff0c\u4e8e\u662f\u6211\u5c31\u5e26\u7740\u4ed6\u6252\u62c9\u4e86\u4e00\u4e0b\u8fd9\u4e2a\u6ce8\u89e3\u7684\u6e90\u7801\uff0c\u8fd8\u6709 <code>SpringApplication<\/code> \u7c7b\u7684 <code>run()<\/code> \u65b9\u6cd5\u7684\u6e90\u7801\uff0c\u4e00\u4e0b\u5b50\u4ed6\u5c31\u660e\u767d\u4e86\u3002<\/p>\n<p>\u4f60\u522b\u8bf4\uff0c\u770b\u6e90\u7801\u7684\u8fc7\u7a0b\u8fd8\u771f\u7684\u662f\u633a\u6709\u8da3\uff0c\u8fd9\u4e0d\uff0c\u6211\u5c31\u53d1\u73b0\u4e86\u4e00\u4e2a\u6709\u610f\u601d\u7684\u70b9\u3002<\/p>\n<pre><code class=\"java\">public ConfigurableApplicationContext run(String... args) {\n    StopWatch stopWatch = new StopWatch();\n    stopWatch.start();\n    ......\n    stopWatch.stop();\n}<\/code><\/pre>\n<p>Spring Boot \u662f\u7528 StopWatch \u6765\u7edf\u8ba1\u8017\u65f6\u7684\uff0c\u800c\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f1a\u7528 <code>System.currentTimeMillis()<\/code> \u6765\u7edf\u8ba1\u8017\u65f6\uff0c\u5bf9\u5427\uff1f\u7f16\u7a0b\u55b5\ud83d\udc31\u5f00\u6e90\u9879\u76ee\u91cc\u5c31\u6709\u8fd9\u6837\u4e00\u6bb5\u4ee3\u7801\uff0c\u5728\u5904\u7406\u7edf\u4e00\u65e5\u5fd7\u5904\u7406\u5207\u9762\u7684\u65f6\u5019\u3002<\/p>\n<pre><code class=\"java\">@Around(&quot;webLog()&quot;)\npublic Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {\n    long startTime = System.currentTimeMillis();\n    long endTime = System.currentTimeMillis();\n    webLog.setSpendTime((int) (endTime - startTime));\n}<\/code><\/pre>\n<p>\u5bf9\u6bd4\u4e4b\u4e0b\uff0c\u6211\u4eec\u5c31\u80fd\u53d1\u73b0\uff0cJDK \u63d0\u4f9b\u7684 <code>System.currentTimeMillis()<\/code> \u6ca1\u6709 Spring \u63d0\u4f9b\u7684 StopWatch \u7b80\u6d01\u3001\u6e05\u6670\u3002<\/p>\n<p>\u5c24\u5176\u662f\u5728\u591a\u4efb\u52a1\u7684\u60c5\u51b5\u4e0b\uff0cStopWatch \u7b80\u76f4\u597d\u7528\u5230\u7206\ud83e\udd17\uff01<\/p>\n<pre><code class=\"java\">\/\/ \u521b\u5efa\u4e00\u4e2a StopWatch \u5b9e\u4f8b\nStopWatch sw = new StopWatch(&quot;\u6c89\u9ed8\u738b\u4e8c\u662f\u50bb X&quot;);\n\/\/ \u5f00\u59cb\u8ba1\u65f6\nsw.start(&quot;\u4efb\u52a11&quot;);\n\nThread.sleep(1000);\n\n\/\/ \u505c\u6b62\u8ba1\u65f6\nsw.stop();\nSystem.out.printf(&quot;\u4efb\u52a11\u8017\u65f6\uff1a%d%s.\/n&quot;, sw.getLastTaskTimeMillis(), &quot;ms&quot;);\n\nsw.start(&quot;\u4efb\u52a12&quot;);\nThread.sleep(1100);\nsw.stop();\n\nSystem.out.printf(&quot;\u4efb\u52a12\u8017\u65f6\uff1a%d%s.\/n&quot;, sw.getLastTaskTimeMillis(), &quot;ms&quot;);\nSystem.out.printf(&quot;\u4efb\u52a1\u6570\u91cf\uff1a%s\uff0c\u603b\u8017\u65f6\uff1a%ss.\/n&quot;, sw.getTaskCount(), sw.getTotalTimeSeconds());<\/code><\/pre>\n<p>\u770b\u5230\u6ca1\uff0c\u662f\u4e0d\u662f\u5f88\u7b80\u5355\uff1f<\/p>\n<ul>\n<li>\u5148 new \u4e00\u4e2a StopWatch \u5bf9\u8c61<\/li>\n<li>\u518d start \u5f00\u59cb\u8ba1\u65f6<\/li>\n<li>\u7136\u540e stop \u505c\u6b62\u8ba1\u65f6<\/li>\n<li>\u6700\u540e\u901a\u8fc7 <code>sw.getLastTaskTimeMillis()<\/code> \u5f97\u51fa\u65f6\u95f4\u5dee<\/li>\n<\/ul>\n<p>\u5982\u679c\u6362\u6210 <code>System.currentTimeMillis()<\/code> \u5c31\u8981\u4e86\u8001\u547d\uff0c\u5148\u5f97\u58f0\u660e\u597d\u51e0\u4e2a long \u578b\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u7136\u540e\u8981\u7b2c\u4e8c\u4e2a\u51cf\u7b2c\u4e00\u4e2a\uff0c\u7b2c\u4e09\u4e2a\u51cf\u7b2c\u4e8c\u4e2a\uff0c\u7a0d\u5fae\u7c97\u5fc3\u4e00\u70b9\uff08\u5c24\u5176\u662f CV \u5927\u6cd5\uff09\u65f6\uff0c\u5f88\u5bb9\u6613\u641e\u9519\u3002<\/p>\n<p>\u9664\u4e86\u53ef\u4ee5\u901a\u8fc7\u5c40\u90e8\u65f6\u95f4\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7 <code>sw.getTotalTimeSeconds()<\/code> \u83b7\u53d6\u603b\u7684\u8017\u65f6\u3002<\/p>\n<pre><code>\u4efb\u52a11\u8017\u65f6\uff1a1002ms.\n\u4efb\u52a12\u8017\u65f6\uff1a1105ms.\n\u4efb\u52a1\u6570\u91cf\uff1a2\uff0c\u603b\u8017\u65f6\uff1a2.107820109s.<\/code><\/pre>\n<p>\u53e6\u5916\uff0cStopWatch \u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2a <code>sw.prettyPrint()<\/code> \u65b9\u6cd5\u4f9b\u6253\u5370\u51fa\u6f02\u4eae\u7684\u683c\u5f0f\u5316\u7ed3\u679c\uff1a<\/p>\n<pre><code>StopWatch '\u6c89\u9ed8\u738b\u4e8c\u662f\u50bb X': running time = 2108529351 ns\n---------------------------------------------\nns         %     Task name\n---------------------------------------------\n1004338467  048%  \u4efb\u52a11\n1104190884  052%  \u4efb\u52a12<\/code><\/pre>\n<p>\u6709\u8017\u65f6\uff0c\u6709\u5360\u7528\u767e\u5206\u6bd4\uff0c\u8fd8\u6709\u4efb\u52a1\u540d\uff0c\u975e\u5e38\u6e05\u6670\u3002<\/p>\n<p>\u9664\u4e86 Spring\uff0chutool \u5de5\u5177\u5e93\u548c Apache common \u5de5\u5177\u5305\u90fd\u63d0\u4f9b\u4e86\u5404\u81ea\u7684 StopWatch\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042229641\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u67e5\u770b hutool \u5de5\u5177\u5e93\u4e2d\u7684 StopWatch \u6e90\u7801\u53ef\u4ee5\u5f97\u51fa\uff0c\u8be5\u7c7b\u5176\u5b9e\u5c31\u6765\u81ea Spring \u7684 StopWatch.java\uff0c\u7528\u6cd5\u4e5f\u5b8c\u5168\u4e00\u81f4\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042229642\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u8fd9\u8bf4\u660e hutool \u7684\u4f5c\u8005\u4e5f\u8ba4\u4e3a Spring \u7684 StopWatch \u5199\u5f97\u597d\uff0c\u54c8\u54c8\u54c8\ud83d\ude01\u3002<\/p>\n<p>\u4f7f\u7528 Beyond compare \u6bd4\u8f83\u540e\u4e5f\u80fd\u5f97\u51fa\uff0c\u4e24\u8005\u9664\u4e86\u4e00\u4e2a\u4e2d\u6587\u6ce8\u91ca\uff0c\u4e00\u4e2a\u82f1\u6587\u6ce8\u91ca\uff0c\u4ee3\u7801\u51e0\u4e4e\u4e00\u6837\u3002setKeepTaskList \u65b9\u6cd5\u6709\u6bd4\u8f83\u5927\u7684\u4e0d\u540c\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042229643\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u90a3\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4f60\u7684\u9879\u76ee\u4e2d\u6ca1\u6709\u4f7f\u7528 Spring \u5168\u5bb6\u6876\uff0c\u53ea\u7528\u4e86 hutool \u5de5\u5177\u5305\uff0c\u90a3\u5c31\u53ef\u4ee5\u4f7f\u7528 hutool \u7684 StopWatch \u6765\u4ee3\u66ff <code>System.currentTimeMillis()<\/code>\u3002<\/p>\n<p>\u901a\u8fc7\u5206\u6790 StopWatch \u7684 stop \u65b9\u6cd5\u6e90\u7801\uff1a<\/p>\n<pre><code class=\"java\">public void stop() throws IllegalStateException {\n    if (null == this.currentTaskName) {\n        throw new IllegalStateException(&quot;Can't stop StopWatch: it's not running&quot;);\n    }\n\n    final long lastTime = System.nanoTime() - this.startTimeNanos;\n    this.totalTimeNanos += lastTime;\n    this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime);\n    if (null != this.taskList) {\n        this.taskList.add(this.lastTaskInfo);\n    }\n    ++this.taskCount;\n    this.currentTaskName = null;\n}<\/code><\/pre>\n<p>\u5176\u5b9e\u53ef\u4ee5\u53d1\u73b0\uff0cStopWatch \u7684\u5185\u90e8\u662f\u901a\u8fc7 <code>System.nanoTime()<\/code> \u6765\u8ba1\u65f6\u7684\uff0c\u672c\u8d28\u4e0a\u548c <code>System.currentTimeMillis()<\/code> \u5dee\u522b\u5e76\u4e0d\u5927\u3002<\/p>\n<p>nanoTime \u6bd4 currentTimeMillis \u7684\u7c92\u5ea6\u66f4\u7ec6\uff0c\u524d\u8005\u662f\u4ee5\u7eb3\u79d2\u4e3a\u5355\u4f4d\uff0c\u540e\u8005\u662f\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042229644\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6ce8\u610f\u4e24\u8005\u90fd\u662f native \u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u503c\u7684\u7c92\u5ea6\u5176\u5b9e\u53d6\u51b3\u4e8e\u5e95\u5c42\u7684\u64cd\u4f5c\u7cfb\u7edf\u3002<\/p>\n<p><strong>\u770b\u5230\u8fd9\uff0c\u5927\u5bb6\u53ef\u80fd\u4f1a\u604d\u7136\u5927\u609f\uff0cStopWatch \u4e0d\u8fc7\u662f\u62ab\u7740\u4e00\u5c42\u5916\u8863\u7684 <code>System.currentTimeMillis()<\/code> \u561b<\/strong>\uff1f<\/p>\n<p>\u4f46\u5999\u5c31\u5999\u5728\uff0c\u8fd9\u5c42\u5916\u8863\u8db3\u591f\u7684\u6f02\u4eae\uff0c\u8db3\u591f\u7684\u4f18\u96c5\u3002StopWatch \u53ef\u4ee5\u8bb0\u5f55\u6bcf\u4e2a\u5b50\u4efb\u52a1\u7684\u540d\u79f0\uff0c\u4ee5\u53ca\u6309\u683c\u5f0f\u5316\u6253\u5370\u7ed3\u679c\uff0c\u5c24\u5176\u662f\u9488\u5bf9\u591a\u4efb\u52a1\u7edf\u8ba1\u65f6\u66f4\u53cb\u597d\u4e00\u70b9\u3002<\/p>\n<p>\u5f53\u7136\u4e86\uff0c\u9664\u4e86\u9009\u62e9 Spring \u548c hutool \u7684 StopWatch\uff0cApache commons-lang3 \u7684 StopWatch \u4e5f\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u53ef\u9009\u9879\uff0c\u66f4\u52a0\u7075\u6d3b\u591a\u53d8\u3002<\/p>\n<pre><code class=\"java\">StopWatch sw = StopWatch.createStarted();\nThread.sleep(1000);\nSystem.out.printf(&quot;\u8017\u65f6\uff1a%dms.\/n&quot;, sw.getTime());<\/code><\/pre>\n<p>\u5176\u4ed6\u4e24\u4e2a\u90fd\u662f\u901a\u8fc7 new \u6765\u521b\u5efa StopWatch \u5bf9\u8c61\uff0ccommons-lang3 \u8fd8\u53ef\u4ee5\u901a\u8fc7 createStarted\uff08\u521b\u5efa\u5e76\u7acb\u5373\u542f\u52a8\uff09\u3001create\uff08\u521b\u5efa\uff09\u6765\u5b8c\u6210\u3002<\/p>\n<p>\u8fd8\u53ef\u4ee5\u8c03\u7528 suspend \u65b9\u6cd5\u6682\u505c\u8ba1\u65f6\u3001resume \u65b9\u6cd5\u6062\u590d\u8ba1\u65f6\u3001reset \u91cd\u65b0\u8ba1\u65f6\u3002<\/p>\n<pre><code class=\"java\">\/\/ \u6682\u505c\u8ba1\u65f6\nsw.suspend();\nSystem.out.printf(&quot;\u6682\u505c\u8017\u65f6\uff1a%dms.\/n&quot;, sw.getTime());\n\n\/\/ \u6062\u590d\u8ba1\u65f6\nsw.resume();\nSystem.out.printf(&quot;\u6062\u590d\u8017\u65f6\uff1a%dms.\/n&quot;, sw.getTime());\n\n\/\/ \u505c\u6b62\u8ba1\u65f6\nsw.stop();\nSystem.out.printf(&quot;\u603b\u8017\u65f6\uff1a%dms.\/n&quot;, sw.getTime());\n\n\/\/ \u91cd\u7f6e\u8ba1\u65f6\nsw.reset();\n\n\/\/ \u5f00\u59cb\u8ba1\u65f6\nsw.start();\nSystem.out.printf(&quot;\u91cd\u7f6e\u8017\u65f6\uff1a%dms.\/n&quot;, sw.getTime());<\/code><\/pre>\n<h2>ending<\/h2>\n<p><em>\u6ca1\u6709\u4ec0\u4e48\u4f7f\u6211\u505c\u7559\u2014\u2014\u9664\u4e86\u76ee\u7684\uff0c\u7eb5\u7136\u5cb8\u65c1\u6709\u73ab\u7470\u3001\u6709\u7eff\u836b\u3001\u6709\u5b81\u9759\u7684\u6e2f\u6e7e\uff0c\u6211\u662f\u4e0d\u7cfb\u4e4b\u821f<\/em>\u3002<\/p>\n<p><strong>\u672c\u6587\u5df2\u6536\u5f55\u5230 GitHub \u4e0a\u661f\u6807 3k+ \u7684\u5f00\u6e90\u4e13\u680f\u300aJava \u7a0b\u5e8f\u5458\u8fdb\u9636\u4e4b\u8def\u300b\uff0c\u636e\u8bf4\u6bcf\u4e00\u4e2a\u4f18\u79c0\u7684 Java \u7a0b\u5e8f\u5458\u90fd\u559c\u6b22\u5979\uff0c\u98ce\u8da3\u5e7d\u9ed8\u3001\u901a\u4fd7\u6613\u61c2\u3002\u5185\u5bb9\u5305\u62ec Java \u57fa\u7840\u3001Java \u5e76\u53d1\u7f16\u7a0b\u3001Java \u865a\u62df\u673a\u3001Java \u4f01\u4e1a\u7ea7\u5f00\u53d1\uff08Git\u3001Nginx\u3001Maven\u3001Intellij IDEA\u3001Spring\u3001Spring Boot\u3001Redis\u3001MySql \u7b49\u7b49\uff09\u3001Java \u9762\u8bd5\u7b49\u6838\u5fc3\u77e5\u8bc6\u70b9\u3002\u5b66 Java\uff0c\u5c31\u8ba4\u51c6 Java \u7a0b\u5e8f\u5458\u8fdb\u9636\u4e4b\u8def<\/strong>\ud83d\ude04\u3002<\/p>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=Arw6bf7tbz0Eb2mFp%2FyLYQ%3D%3D.8aigokVC8wPGkUhPBUgaTrQ1e2RLqysNh9d4f0rkFC6rq6mvnwJwS9kFGZBojYoo\" rel=\"nofollow\">https:\/\/github.com\/itwanger\/toBeBetterJavaer<\/a><\/p>\n<p>star \u4e86\u8fd9\u4e2a\u4ed3\u5e93\u5c31\u7b49\u4e8e\u4f60\u62e5\u6709\u4e86\u6210\u4e3a\u4e86\u4e00\u540d\u4f18\u79c0 Java \u5de5\u7a0b\u5e08\u7684\u6f5c\u529b\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042229645\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042229639\">https:\/\/segmentfault.com\/a\/1190000042229639<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u4e8c\u54e5\u5440\uff01 \u6628\u5929\uff0c\u4e00\u4f4d\u7403\u53cb\u95ee\u6211\u80fd\u4e0d\u80fd\u7ed9\u4ed6\u89e3\u91ca\u4e00\u4e0b @SpringBootApplication \u6ce8\u89e3 [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006037","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006037","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006037"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006037\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}