{"id":1127525,"date":"2025-01-08T20:11:05","date_gmt":"2025-01-08T12:11:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1127525.html"},"modified":"2025-01-08T20:11:07","modified_gmt":"2025-01-08T12:11:07","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97%e7%a8%8b%e5%ba%8f%e8%bf%90%e8%a1%8c%e4%ba%86%e5%a4%9a%e9%95%bf%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1127525.html","title":{"rendered":"python\u5982\u4f55\u8ba1\u7b97\u7a0b\u5e8f\u8fd0\u884c\u4e86\u591a\u957f\u65f6\u95f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094526\/d1aa7787-6911-4a38-aa5b-3b3b06e9ab78.webp\" alt=\"python\u5982\u4f55\u8ba1\u7b97\u7a0b\u5e8f\u8fd0\u884c\u4e86\u591a\u957f\u65f6\u95f4\" \/><\/p>\n<p><p> <strong>Python \u8ba1\u7b97\u7a0b\u5e8f\u8fd0\u884c\u65f6\u957f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709\uff1a\u4f7f\u7528 <code>time<\/code> \u6a21\u5757\u3001\u4f7f\u7528 <code>datetime<\/code> \u6a21\u5757\u3001\u4f7f\u7528 <code>timeit<\/code> \u6a21\u5757\u3001\u4f7f\u7528 <code>perf_counter<\/code> \u51fd\u6570\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u662f <code>time<\/code> \u6a21\u5757\u548c <code>timeit<\/code> \u6a21\u5757\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u6027\u80fd\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>time<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>time<\/code> \u6a21\u5757\u662f Python \u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u4e0e\u65f6\u95f4\u76f8\u5173\u7684\u51fd\u6570\u3002\u901a\u8fc7\u8bb0\u5f55\u7a0b\u5e8f\u5f00\u59cb\u548c\u7ed3\u675f\u65f6\u7684\u65f6\u95f4\u6233\uff0c\u53ef\u4ee5\u8ba1\u7b97\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u957f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bb0\u5f55\u65f6\u95f4\u6233<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 <code>time.time()<\/code> \u51fd\u6570\u8bb0\u5f55\u7a0b\u5e8f\u5f00\u59cb\u548c\u7ed3\u675f\u7684\u65f6\u95f4\u6233\uff0c\u7136\u540e\u76f8\u51cf\u5f97\u5230\u8fd0\u884c\u65f6\u957f\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>start_time = time.time()<\/p>\n<h2><strong>\u6a21\u62df\u7a0b\u5e8f\u8fd0\u884c<\/strong><\/h2>\n<p>time.sleep(2)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c<\/p>\n<p>end_time = time.time()<\/p>\n<p>elapsed_time = end_time - start_time<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\uff1a{elapsed_time} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bb0\u5f55\u7cbe\u786e\u65f6\u95f4<\/h4>\n<\/p>\n<p><p><code>time<\/code> \u6a21\u5757\u4e2d\u7684 <code>time.perf_counter()<\/code> \u51fd\u6570\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7cbe\u5ea6\u7684\u65f6\u95f4\u6d4b\u91cf\uff0c\u9002\u7528\u4e8e\u9700\u8981\u7cbe\u786e\u6d4b\u91cf\u65f6\u95f4\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>start_time = time.perf_counter()<\/p>\n<h2><strong>\u6a21\u62df\u7a0b\u5e8f\u8fd0\u884c<\/strong><\/h2>\n<p>time.sleep(2)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c<\/p>\n<p>end_time = time.perf_counter()<\/p>\n<p>elapsed_time = end_time - start_time<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\uff1a{elapsed_time:.6f} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>datetime<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>datetime<\/code> \u6a21\u5757\u4e5f\u662f Python \u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u7c7b\u3002\u901a\u8fc7\u8ba1\u7b97 <code>datetime<\/code> \u5bf9\u8c61\u7684\u5dee\u503c\uff0c\u53ef\u4ee5\u8ba1\u7b97\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u957f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528 <code>datetime.now()<\/code><\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8bb0\u5f55\u7a0b\u5e8f\u5f00\u59cb\u548c\u7ed3\u675f\u65f6\u7684 <code>datetime<\/code> \u5bf9\u8c61\uff0c\u5e76\u8ba1\u7b97\u5b83\u4eec\u7684\u5dee\u503c\uff0c\u53ef\u4ee5\u5f97\u5230\u8fd0\u884c\u65f6\u957f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>start_time = datetime.now()<\/p>\n<h2><strong>\u6a21\u62df\u7a0b\u5e8f\u8fd0\u884c<\/strong><\/h2>\n<p>time.sleep(2)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c<\/p>\n<p>end_time = datetime.now()<\/p>\n<p>elapsed_time = end_time - start_time<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\uff1a{elapsed_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528 <code>timedelta<\/code> \u5bf9\u8c61<\/h4>\n<\/p>\n<p><p><code>timedelta<\/code> \u5bf9\u8c61\u8868\u793a\u4e24\u4e2a <code>datetime<\/code> \u5bf9\u8c61\u4e4b\u95f4\u7684\u65f6\u95f4\u5dee\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u83b7\u53d6\u8fd0\u884c\u65f6\u957f\u7684\u79d2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>start_time = datetime.now()<\/p>\n<h2><strong>\u6a21\u62df\u7a0b\u5e8f\u8fd0\u884c<\/strong><\/h2>\n<p>time.sleep(2)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c<\/p>\n<p>end_time = datetime.now()<\/p>\n<p>elapsed_time = end_time - start_time<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\uff1a{elapsed_time.total_seconds()} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 <code>timeit<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>timeit<\/code> \u6a21\u5757\u662f Python \u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u4e13\u95e8\u7528\u4e8e\u6d4b\u91cf\u5c0f\u6bb5\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\u3002\u5b83\u53ef\u4ee5\u81ea\u52a8\u8fdb\u884c\u591a\u6b21\u6d4b\u91cf\uff0c\u5e76\u8fd4\u56de\u5e73\u5747\u65f6\u95f4\uff0c\u9002\u7528\u4e8e\u9700\u8981\u7cbe\u786e\u6d4b\u91cf\u5c0f\u6bb5\u4ee3\u7801\u6027\u80fd\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528 <code>timeit.timeit()<\/code> \u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u4f20\u9012\u4e00\u6bb5\u4ee3\u7801\u5b57\u7b26\u4e32\u548c\u6267\u884c\u6b21\u6570\uff0c<code>timeit.timeit()<\/code> \u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u4ee3\u7801\u7684\u5e73\u5747\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>code_to_test = &quot;&quot;&quot;<\/p>\n<p>a = 10<\/p>\n<p>b = 20<\/p>\n<p>c = a + b<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>elapsed_time = timeit.timeit(code_to_test, number=1000000)<\/p>\n<p>print(f&quot;\u4ee3\u7801\u6267\u884c\u65f6\u95f4\uff1a{elapsed_time} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528 <code>timeit<\/code> \u6a21\u5757\u7684\u547d\u4ee4\u884c\u63a5\u53e3<\/h4>\n<\/p>\n<p><p><code>timeit<\/code> \u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u547d\u4ee4\u884c\u63a5\u53e3\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u6d4b\u91cf\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m timeit -n 1000000 &quot;a=10; b=20; c=a+b&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528 <code>perf_counter<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>perf_counter<\/code> \u662f <code>time<\/code> \u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ad8\u7cbe\u5ea6\u7684\u8ba1\u65f6\u5668\uff0c\u9002\u7528\u4e8e\u9700\u8981\u7cbe\u786e\u6d4b\u91cf\u65f6\u95f4\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528 <code>time.perf_counter()<\/code><\/h4>\n<\/p>\n<p><p>\u4e0e <code>time.time()<\/code> \u7c7b\u4f3c\uff0c\u901a\u8fc7\u8bb0\u5f55\u7a0b\u5e8f\u5f00\u59cb\u548c\u7ed3\u675f\u65f6\u7684\u65f6\u95f4\u6233\uff0c\u53ef\u4ee5\u8ba1\u7b97\u8fd0\u884c\u65f6\u957f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>start_time = time.perf_counter()<\/p>\n<h2><strong>\u6a21\u62df\u7a0b\u5e8f\u8fd0\u884c<\/strong><\/h2>\n<p>time.sleep(2)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c<\/p>\n<p>end_time = time.perf_counter()<\/p>\n<p>elapsed_time = end_time - start_time<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\uff1a{elapsed_time:.6f} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0cPython \u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u957f\u3002<strong>\u5bf9\u4e8e\u5927\u591a\u6570\u573a\u666f\uff0c\u4f7f\u7528 <code>time<\/code> \u6a21\u5757\u7684 <code>time.time()<\/code> \u6216 <code>time.perf_counter()<\/code> \u51fd\u6570\u5373\u53ef\u6ee1\u8db3\u9700\u6c42<\/strong>\u3002<strong>\u5982\u679c\u9700\u8981\u7cbe\u786e\u6d4b\u91cf\u5c0f\u6bb5\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>timeit<\/code> \u6a21\u5757<\/strong>\u3002<strong>\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>datetime<\/code> \u6a21\u5757<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u4f18\u5316\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6d4b\u91cf\u4ee3\u7801\u5757\u7684\u6267\u884c\u65f6\u95f4\uff1f<\/strong><br \/>\u8981\u6d4b\u91cf\u4ee3\u7801\u5757\u7684\u6267\u884c\u65f6\u95f4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u4e2d\u7684<code>time()<\/code>\u51fd\u6570\u3002\u9996\u5148\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4\uff0c\u6267\u884c\u4ee3\u7801\u5757\uff0c\u968f\u540e\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4\u3002\u901a\u8fc7\u8ba1\u7b97\u7ed3\u675f\u65f6\u95f4\u4e0e\u5f00\u59cb\u65f6\u95f4\u7684\u5dee\u503c\uff0c\u5373\u53ef\u83b7\u5f97\u4ee3\u7801\u5757\u7684\u6267\u884c\u65f6\u95f4\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import time\n\nstart_time = time.time()\n# \u5728\u6b64\u5904\u653e\u7f6e\u8981\u6d4b\u91cf\u7684\u4ee3\u7801\nend_time = time.time()\n\nprint(f&quot;\u6267\u884c\u65f6\u95f4\u4e3a: {end_time - start_time} \u79d2&quot;)\n<\/code><\/pre>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u66f4\u51c6\u786e\u5730\u8ba1\u7b97\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>time<\/code>\u6a21\u5757\uff0c<code>timeit<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u7cbe\u51c6\u7684\u6d4b\u91cf\u5de5\u5177\u3002\u5b83\u81ea\u52a8\u591a\u6b21\u8fd0\u884c\u4ee3\u7801\u5e76\u8ba1\u7b97\u5e73\u5747\u6267\u884c\u65f6\u95f4\uff0c\u9002\u5408\u6027\u80fd\u6d4b\u8bd5\u3002\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import timeit\n\nexecution_time = timeit.timeit(&#39;your_code_here&#39;, number=1000)\nprint(f&quot;\u5e73\u5747\u6267\u884c\u65f6\u95f4\u4e3a: {execution_time \/ 1000} \u79d2&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u66f4\u597d\u5730\u8bc4\u4f30\u4ee3\u7801\u7684\u6027\u80fd\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\u5e76\u8f93\u51fa\u5230\u65e5\u5fd7\u6587\u4ef6\uff1f<\/strong><br \/>\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u5e76\u5c06\u5176\u8f93\u51fa\u5230\u65e5\u5fd7\u6587\u4ef6\u3002\u60a8\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u65e5\u5fd7\u683c\u5f0f\u548c\u6587\u4ef6\u8def\u5f84\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import logging\nimport time\n\nlogging.basicConfig(filename=&#39;execution_time.log&#39;, level=logging.INFO)\n\nstart_time = time.time()\n# \u6267\u884c\u4ee3\u7801\nend_time = time.time()\n\nexecution_duration = end_time - start_time\nlogging.info(f&quot;\u7a0b\u5e8f\u6267\u884c\u65f6\u95f4: {execution_duration} \u79d2&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8ffd\u8e2a\u7a0b\u5e8f\u7684\u6027\u80fd\u5e76\u4fdd\u5b58\u8bb0\u5f55\u4ee5\u4fbf\u540e\u7eed\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u8ba1\u7b97\u7a0b\u5e8f\u8fd0\u884c\u65f6\u957f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709\uff1a\u4f7f\u7528 time \u6a21\u5757\u3001\u4f7f\u7528 datetime \u6a21\u5757\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1127532,"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\/1127525"}],"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=1127525"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127525\/revisions"}],"predecessor-version":[{"id":1127535,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127525\/revisions\/1127535"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1127532"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1127525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1127525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1127525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}