{"id":972413,"date":"2024-12-27T05:48:31","date_gmt":"2024-12-26T21:48:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/972413.html"},"modified":"2024-12-27T05:48:33","modified_gmt":"2024-12-26T21:48:33","slug":"clock%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e7%94%a8python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/972413.html","title":{"rendered":"clock\u51fd\u6570\u5982\u4f55\u7528python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195103\/3219542b-8ba4-4fe0-82b0-4e0472754d2f.webp\" alt=\"clock\u51fd\u6570\u5982\u4f55\u7528python\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong><code>clock<\/code>\u51fd\u6570\u7528\u4e8e\u6d4b\u91cf\u4ee3\u7801\u6267\u884c\u7684\u65f6\u95f4<\/strong>\u3002\u7136\u800c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4ecePython 3.3\u7248\u672c\u5f00\u59cb\uff0c<code>clock<\/code>\u51fd\u6570\u5df2\u88ab\u6807\u8bb0\u4e3a\u8fc7\u65f6\u5e76\u5728Python 3.8\u4e2d\u88ab\u79fb\u9664\u3002\u73b0\u5728\u63a8\u8350\u4f7f\u7528\u5176\u4ed6\u66f4\u7cbe\u786e\u548c\u8de8\u5e73\u53f0\u7684\u65f6\u95f4\u6d4b\u91cf\u51fd\u6570\uff0c\u5982<code>perf_counter()<\/code>\u6216<code>process_time()<\/code>\u3002<strong><code>perf_counter()<\/code>\u9002\u7528\u4e8e\u6d4b\u91cf\u77ed\u65f6\u95f4\u95f4\u9694\u7684\u7cbe\u786e\u65f6\u95f4\uff0c\u800c<code>process_time()<\/code>\u5219\u66f4\u9002\u5408\u7528\u4e8e\u6d4b\u91cfCPU\u6267\u884c\u65f6\u95f4\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>perf_counter()<\/code>\u548c<code>process_time()<\/code>\u51fd\u6570\u6765\u66ff\u4ee3<code>clock<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528PERF_COUNTER()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>perf_counter()<\/code>\u63d0\u4f9b\u4e86\u9ad8\u5206\u8fa8\u7387\u7684\u65f6\u95f4\u8ba1\u6570\u5668\uff0c\u9002\u5408\u7528\u4e8e\u6d4b\u91cf\u8fd0\u884c\u65f6\u95f4\u95f4\u9694\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u8868\u793a\u81ea\u67d0\u4e2a\u7279\u5b9a\u65f6\u95f4\u70b9\uff08\u901a\u5e38\u662f\u7a0b\u5e8f\u542f\u52a8\u65f6\uff09\u6240\u7ecf\u8fc7\u7684\u79d2\u6570\u3002\u4e0e<code>clock<\/code>\u51fd\u6570\u4e0d\u540c\uff0c<code>perf_counter()<\/code>\u5305\u542b\u4e86\u7761\u7720\u65f6\u95f4\uff0c\u56e0\u6b64\u66f4\u9002\u5408\u4e8e\u6d4b\u91cf\u7a0b\u5e8f\u7684\u6574\u4f53\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/h4>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165\u6a21\u5757<\/strong>\uff1a\u9996\u5148\u9700\u8981\u5bfc\u5165<code>time<\/code>\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bb0\u5f55\u8d77\u59cb\u65f6\u95f4<\/strong>\uff1a\u5728\u9700\u8981\u6d4b\u91cf\u7684\u4ee3\u7801\u6bb5\u5f00\u59cb\u5904\u8c03\u7528<code>perf_counter()<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">start_time = time.perf_counter()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6267\u884c\u4ee3\u7801<\/strong>\uff1a\u7f16\u5199\u9700\u8981\u6d4b\u91cf\u65f6\u95f4\u7684\u4ee3\u7801\u6bb5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>for i in range(1000):<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4\u5e76\u8ba1\u7b97\u5dee\u503c<\/strong>\uff1a\u5728\u4ee3\u7801\u6bb5\u7ed3\u675f\u5904\u518d\u6b21\u8c03\u7528<code>perf_counter()<\/code>\uff0c\u7136\u540e\u8ba1\u7b97\u65f6\u95f4\u5dee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">end_time = time.perf_counter()<\/p>\n<p>elapsed_time = end_time - start_time<\/p>\n<p>print(f&quot;Elapsed time: {elapsed_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528PROCESS_TIME()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>process_time()<\/code>\u7528\u4e8e\u6d4b\u91cf\u5904\u7406\u5668\u6267\u884c\u65f6\u95f4\uff0c\u4e0d\u5305\u62ec\u7761\u7720\u65f6\u95f4\uff0c\u56e0\u6b64\u5b83\u66f4\u9002\u5408\u7528\u4e8e\u6d4b\u91cf\u7a0b\u5e8f\u7684CPU\u4f7f\u7528\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/h4>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165\u6a21\u5757<\/strong>\uff1a\u540c\u6837\u9700\u8981\u5148\u5bfc\u5165<code>time<\/code>\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bb0\u5f55\u8d77\u59cb\u65f6\u95f4<\/strong>\uff1a\u5728\u9700\u8981\u6d4b\u91cf\u7684\u4ee3\u7801\u6bb5\u5f00\u59cb\u5904\u8c03\u7528<code>process_time()<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">start_time = time.process_time()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6267\u884c\u4ee3\u7801<\/strong>\uff1a\u7f16\u5199\u9700\u8981\u6d4b\u91cf\u65f6\u95f4\u7684\u4ee3\u7801\u6bb5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>for i in range(1000):<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4\u5e76\u8ba1\u7b97\u5dee\u503c<\/strong>\uff1a\u5728\u4ee3\u7801\u6bb5\u7ed3\u675f\u5904\u518d\u6b21\u8c03\u7528<code>process_time()<\/code>\uff0c\u7136\u540e\u8ba1\u7b97\u65f6\u95f4\u5dee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">end_time = time.process_time()<\/p>\n<p>elapsed_time = end_time - start_time<\/p>\n<p>print(f&quot;CPU time: {elapsed_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001PERF_COUNTER()\u4e0ePROCESS_TIME()\u7684\u533a\u522b<\/h3>\n<\/p>\n<ul>\n<li>\n<p><strong>\u7cbe\u5ea6\u4e0e\u7528\u9014<\/strong>\uff1a<code>perf_counter()<\/code>\u63d0\u4f9b\u66f4\u9ad8\u7684\u7cbe\u5ea6\uff0c\u9002\u7528\u4e8e\u6d4b\u91cf\u7a0b\u5e8f\u7684\u603b\u4f53\u8fd0\u884c\u65f6\u95f4\uff0c\u5305\u62ecI\/O\u64cd\u4f5c\u548c\u7761\u7720\u65f6\u95f4\u3002\u800c<code>process_time()<\/code>\u53ea\u8ba1\u7b97\u7a0b\u5e8f\u5728CPU\u4e0a\u8fd0\u884c\u7684\u65f6\u95f4\uff0c\u4e0d\u5305\u62ec\u7b49\u5f85\u548c\u7761\u7720\u65f6\u95f4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e73\u53f0\u4f9d\u8d56\u6027<\/strong>\uff1a<code>perf_counter()<\/code>\u662f\u8de8\u5e73\u53f0\u7684\uff0c\u80fd\u591f\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u63d0\u4f9b\u4e00\u81f4\u7684\u65f6\u95f4\u6d4b\u91cf\u3002\u800c<code>process_time()<\/code>\u4e5f\u5177\u6709\u8de8\u5e73\u53f0\u7279\u6027\uff0c\u4f46\u5176\u7528\u9014\u66f4\u4e3a\u4e13\u4e00\uff0c\u4e13\u6ce8\u4e8eCPU\u65f6\u95f4\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u5176\u4ed6\u65f6\u95f4\u6d4b\u91cf\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86<code>perf_counter()<\/code>\u548c<code>process_time()<\/code>\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u65f6\u95f4\u6d4b\u91cf\u65b9\u6cd5\uff0c\u5982<code>timeit<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u7528\u4e8e\u66f4\u7cbe\u786e\u7684\u6027\u80fd\u6d4b\u8bd5\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>timeit\u6a21\u5757<\/strong>\uff1a<code>timeit<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7cbe\u786e\u7684\u65f6\u95f4\u6d4b\u91cf\u65b9\u5f0f\uff0c\u80fd\u591f\u81ea\u52a8\u6267\u884c\u591a\u6b21\u4ee5\u51cf\u5c11\u8fd0\u884c\u65f6\u95f4\u7684\u968f\u673a\u8bef\u5dee\u3002\u9002\u7528\u4e8e\u9700\u8981\u6d4b\u91cf\u5c0f\u4ee3\u7801\u6bb5\u7684\u6267\u884c\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<p><h4>\u4f7f\u7528timeit\u6a21\u5757\u7684\u57fa\u672c\u65b9\u6cd5\uff1a<\/h4>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165\u6a21\u5757<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9a\u4e49\u8981\u6d4b\u8bd5\u7684\u4ee3\u7801<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">code_to_test = &quot;&quot;&quot;<\/p>\n<p>a = range(1000)<\/p>\n<p>b = [x*2 for x in a]<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6267\u884c\u6d4b\u8bd5\u5e76\u8f93\u51fa\u7ed3\u679c<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">elapsed_time = timeit.timeit(code_to_test, number=1000)<\/p>\n<p>print(f&quot;Time taken for execution: {elapsed_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u6709\u6548\u5730\u6d4b\u91cf\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\uff0c\u4ece\u800c\u4f18\u5316\u7a0b\u5e8f\u6027\u80fd\u3002<code>perf_counter()<\/code>\u548c<code>process_time()<\/code>\u51fd\u6570\u662f<code>clock<\/code>\u51fd\u6570\u7684\u7406\u60f3\u66ff\u4ee3\u65b9\u6848\uff0c\u80fd\u591f\u63d0\u4f9b\u66f4\u9ad8\u7684\u7cbe\u5ea6\u548c\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528clock\u51fd\u6570\u6765\u8ba1\u65f6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u867d\u7136\u539f\u751f\u7684<code>clock()<\/code>\u51fd\u6570\u5728Python 3.3\u7248\u672c\u540e\u88ab\u5f03\u7528\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u4e2d\u7684<code>time()<\/code>\u6216<code>perf_counter()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002<code>time()<\/code>\u51fd\u6570\u8fd4\u56de\u5f53\u524d\u7684\u65f6\u95f4\u6233\uff0c\u800c<code>perf_counter()<\/code>\u51fd\u6570\u63d0\u4f9b\u66f4\u9ad8\u7cbe\u5ea6\u7684\u8ba1\u65f6\u529f\u80fd\uff0c\u9002\u5408\u7528\u4e8e\u6027\u80fd\u6d4b\u91cf\u3002<\/p>\n<p><strong>\u4f7f\u7528clock\u51fd\u6570\u8ba1\u65f6\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528<code>perf_counter()<\/code>\u6216<code>process_time()<\/code>\u6765\u66ff\u4ee3<code>clock()<\/code>\u3002<code>perf_counter()<\/code>\u9002\u5408\u4e8e\u6d4b\u91cf\u8fd0\u884c\u65f6\u95f4\uff0c\u56e0\u4e3a\u5b83\u5305\u62ec\u7761\u7720\u65f6\u95f4\uff0c\u800c<code>process_time()<\/code>\u5219\u53ea\u8ba1\u7b97\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u95f4\u3002\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u524d\u540e\u8bb0\u5f55\u65f6\u95f4\uff0c\u8ba1\u7b97\u4e24\u8005\u7684\u5dee\u503c\u6765\u83b7\u53d6\u8fd0\u884c\u65f6\u957f\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u6ca1\u6709\u5176\u4ed6\u63a8\u8350\u7684\u8ba1\u65f6\u65b9\u5f0f\uff1f<\/strong><br \/>\u9664\u4e86<code>time<\/code>\u6a21\u5757\uff0cPython\u7684<code>datetime<\/code>\u6a21\u5757\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u65f6\u95f4\u548c\u65e5\u671f\uff0c\u9002\u5408\u4e8e\u8bb0\u5f55\u4e8b\u4ef6\u53d1\u751f\u7684\u5177\u4f53\u65f6\u95f4\u3002\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u7684\u8ba1\u65f6\u529f\u80fd\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757\uff0c\u5b83\u4e13\u95e8\u4e3a\u6027\u80fd\u6d4b\u8bd5\u8bbe\u8ba1\uff0c\u53ef\u4ee5\u81ea\u52a8\u8fdb\u884c\u591a\u6b21\u6d4b\u91cf\u5e76\u8fd4\u56de\u7ed3\u679c\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u4f18\u5316\u4ee3\u7801\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0cclock\u51fd\u6570\u7528\u4e8e\u6d4b\u91cf\u4ee3\u7801\u6267\u884c\u7684\u65f6\u95f4\u3002\u7136\u800c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4ecePython 3.3\u7248\u672c\u5f00\u59cb\uff0ccl [&hellip;]","protected":false},"author":3,"featured_media":972417,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972413"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=972413"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972413\/revisions"}],"predecessor-version":[{"id":972418,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972413\/revisions\/972418"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/972417"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=972413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=972413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=972413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}