{"id":926560,"date":"2024-12-26T15:53:07","date_gmt":"2024-12-26T07:53:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/926560.html"},"modified":"2024-12-26T15:53:09","modified_gmt":"2024-12-26T07:53:09","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a1%e6%97%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/926560.html","title":{"rendered":"python\u5982\u4f55\u8ba1\u65f6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063118\/f30283ec-7e54-49d3-b0b9-b998eed6e947.webp\" alt=\"python\u5982\u4f55\u8ba1\u65f6\" \/><\/p>\n<p><p> <strong>Python\u8ba1\u65f6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528time\u6a21\u5757\u3001datetime\u6a21\u5757\u3001timeit\u6a21\u5757\u7b49\u3002time\u6a21\u5757\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u8ba1\u65f6\u529f\u80fd\u3001datetime\u6a21\u5757\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u65f6\u95f4\u8ba1\u7b97\u3001timeit\u6a21\u5757\u4e13\u6ce8\u4e8e\u4ee3\u7801\u6027\u80fd\u6d4b\u8bd5\u3002<\/strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u6a21\u5757\u548c\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u6a21\u5757\u8fdb\u884c\u8ba1\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001TIME\u6a21\u5757<\/p>\n<\/p>\n<p><p>time\u6a21\u5757\u662fPython\u4e2d\u7528\u4e8e\u5904\u7406\u65f6\u95f4\u76f8\u5173\u4efb\u52a1\u7684\u6807\u51c6\u6a21\u5757\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u8ba1\u65f6\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528time()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>time()<\/code>\u51fd\u6570\u8fd4\u56de\u81ea\u7eaa\u5143\u4ee5\u6765\u7684\u79d2\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u7b80\u5355\u7684\u8ba1\u65f6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>start_time = time.time()<\/p>\n<h2><strong>\u6267\u884c\u4e00\u4e9b\u4ee3\u7801<\/strong><\/h2>\n<p>end_time = time.time()<\/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<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>time.time()<\/code>\u8bb0\u5f55\u4ee3\u7801\u5f00\u59cb\u548c\u7ed3\u675f\u7684\u65f6\u95f4\uff0c\u7136\u540e\u8ba1\u7b97\u51fa\u4e24\u8005\u7684\u5dee\u503c\u4ee5\u83b7\u5f97\u7ecf\u8fc7\u7684\u79d2\u6570\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u4f46\u7531\u4e8e\u8fd4\u56de\u7684\u65f6\u95f4\u662f\u6d6e\u70b9\u6570\uff0c\u53ef\u80fd\u4f1a\u53d7\u5230\u7cfb\u7edf\u65f6\u949f\u7cbe\u5ea6\u7684\u5f71\u54cd\u3002<\/strong><\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528perf_counter()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>perf_counter()<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7cbe\u5ea6\u7684\u8ba1\u65f6\u529f\u80fd\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u9ad8\u7cbe\u5ea6\u7684\u65f6\u95f4\u6d4b\u91cf\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>\u6267\u884c\u4e00\u4e9b\u4ee3\u7801<\/strong><\/h2>\n<p>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<p><p><code>perf_counter()<\/code>\u53ef\u4ee5\u63d0\u4f9b\u7eb3\u79d2\u7ea7\u522b\u7684\u7cbe\u5ea6\uff0c\u56e0\u6b64\u5728\u9700\u8981\u66f4\u9ad8\u7cbe\u5ea6\u7684\u573a\u5408\u4e0b\u63a8\u8350\u4f7f\u7528\u6b64\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001DATETIME\u6a21\u5757<\/p>\n<\/p>\n<p><p>datetime\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65f6\u95f4\u548c\u65e5\u671f\u5904\u7406\u529f\u80fd\uff0c\u9002\u5408\u7528\u4e8e\u590d\u6742\u7684\u65f6\u95f4\u8ba1\u7b97\u548c\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<p><h3>1. \u8ba1\u7b97\u65f6\u95f4\u5dee<\/h3>\n<\/p>\n<p><p>datetime\u6a21\u5757\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u70b9\u4e4b\u95f4\u7684\u5dee\u503c\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>\u6267\u884c\u4e00\u4e9b\u4ee3\u7801<\/strong><\/h2>\n<p>end_time = datetime.now()<\/p>\n<p>elapsed_time = end_time - start_time<\/p>\n<p>print(f&quot;Elapsed time: {elapsed_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>datetime.now()<\/code>\u8fd4\u56de\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u8ba1\u7b97\u4e24\u4e2adatetime\u5bf9\u8c61\u7684\u5dee\u503c\u4f1a\u8fd4\u56de\u4e00\u4e2a<code>timedelta<\/code>\u5bf9\u8c61\uff0c\u8868\u793a\u4e24\u4e2a\u65f6\u95f4\u70b9\u7684\u5dee\u3002<\/p>\n<\/p>\n<p><h3>2. \u683c\u5f0f\u5316\u65f6\u95f4\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>datetime\u6a21\u5757\u8fd8\u53ef\u4ee5\u7528\u6765\u683c\u5f0f\u5316\u65f6\u95f4\u8f93\u51fa\uff0c\u4f7f\u5176\u66f4\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>now = datetime.now()<\/p>\n<p>formatted_time = now.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(f&quot;Current time: {formatted_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u4f7f\u7528<code>strftime()<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06datetime\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u751f\u6210\u65e5\u5fd7\u65f6\u95f4\u6233\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e09\u3001TIMEIT\u6a21\u5757<\/p>\n<\/p>\n<p><p>timeit\u6a21\u5757\u4e13\u95e8\u7528\u4e8e\u6d4b\u91cf\u5c0f\u6bb5\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\uff0c\u5e38\u7528\u4e8e\u6027\u80fd\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528timeit.timeit()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>timeit.timeit()<\/code>\u51fd\u6570\u53ef\u4ee5\u591a\u6b21\u6267\u884c\u4ee3\u7801\u6bb5\u4ee5\u83b7\u5f97\u66f4\u51c6\u786e\u7684\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>def test_function():<\/p>\n<p>    # \u4e00\u4e9b\u4ee3\u7801<\/p>\n<p>    pass<\/p>\n<p>elapsed_time = timeit.timeit(test_function, number=1000)<\/p>\n<p>print(f&quot;Average time per execution: {elapsed_time \/ 1000} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6307\u5b9a<code>number<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4ee3\u7801\u6bb5\u6267\u884c\u7684\u6b21\u6570\uff0c\u4ee5\u4fbf\u83b7\u5f97\u5e73\u5747\u6267\u884c\u65f6\u95f4\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u7528\u4e8e\u4f18\u5316\u4ee3\u7801\u6027\u80fd\u65f6\u4f7f\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528timeit.Timer\u7c7b<\/h3>\n<\/p>\n<p><p><code>timeit.Timer<\/code>\u7c7b\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u8ba1\u65f6\u529f\u80fd\uff0c\u5141\u8bb8\u7528\u6237\u81ea\u5b9a\u4e49\u8ba1\u65f6\u7684\u4ee3\u7801\u6bb5\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 = 1<\/p>\n<p>b = 2<\/p>\n<p>c = a + b<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>timer = timeit.Timer(stmt=code_to_test)<\/p>\n<p>elapsed_time = timer.timeit(number=1000)<\/p>\n<p>print(f&quot;Average time per execution: {elapsed_time \/ 1000} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06\u4ee3\u7801\u4f5c\u4e3a\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9<code>Timer<\/code>\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5b9a\u4e49\u9700\u8981\u6d4b\u8bd5\u7684\u4ee3\u7801\u5757\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u4e8e\u9700\u8981\u6d4b\u8bd5\u591a\u6bb5\u4ee3\u7801\u6216\u8005\u9700\u8981\u81ea\u5b9a\u4e49\u6d4b\u8bd5\u73af\u5883\u7684\u573a\u5408\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u8ba1\u65f6\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u8ba1\u65f6\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u7684<code>time()<\/code>\u51fd\u6570\uff1b\u5bf9\u4e8e\u9700\u8981\u9ad8\u7cbe\u5ea6\u7684\u8ba1\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>perf_counter()<\/code>\uff1b\u5bf9\u4e8e\u9700\u8981\u590d\u6742\u65f6\u95f4\u8ba1\u7b97\u548c\u683c\u5f0f\u5316\u7684\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\uff1b\u5bf9\u4e8e\u6027\u80fd\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757\u3002<strong>\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u8ba1\u65f6\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u8fdb\u884c\u65f6\u95f4\u7ba1\u7406\u548c\u6027\u80fd\u4f18\u5316\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u65f6\u95f4\u6a21\u5757\u8fdb\u884c\u7b80\u5355\u8ba1\u65f6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u7684<code>time<\/code>\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8ba1\u65f6\u529f\u80fd\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>time.time()<\/code>\u51fd\u6570\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\uff0c\u7136\u540e\u901a\u8fc7\u8ba1\u7b97\u4e24\u6b21\u8c03\u7528\u7684\u65f6\u95f4\u5dee\u6765\u5f97\u5230\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\u6240\u9700\u7684\u65f6\u95f4\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import time\n\nstart_time = time.time()\n# \u8fd9\u91cc\u653e\u7f6e\u4f60\u60f3\u8ba1\u65f6\u7684\u4ee3\u7801\nend_time = time.time()\n\nexecution_time = end_time - start_time\nprint(f&quot;\u4ee3\u7801\u6267\u884c\u65f6\u95f4\u4e3a\uff1a{execution_time}\u79d2&quot;)\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u6709\u6ca1\u6709\u66f4\u65b9\u4fbf\u7684\u8ba1\u65f6\u5de5\u5177\uff1f<\/strong><br \/>\u9664\u4e86<code>time<\/code>\u6a21\u5757\uff0cPython\u7684<code>timeit<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u4e3a\u7cbe\u786e\u7684\u8ba1\u65f6\u65b9\u5f0f\uff0c\u5c24\u5176\u9002\u5408\u7528\u4e8e\u6027\u80fd\u6d4b\u8bd5\u3002<code>timeit<\/code>\u53ef\u4ee5\u81ea\u52a8\u591a\u6b21\u8fd0\u884c\u4ee3\u7801\u5e76\u8ba1\u7b97\u5e73\u5747\u6267\u884c\u65f6\u95f4\u3002\u4f7f\u7528\u65f6\u53ea\u9700\u7b80\u5355\u8c03\u7528<code>timeit.timeit()<\/code>\u51fd\u6570\uff0c\u4f20\u5165\u4f60\u60f3\u8981\u8ba1\u65f6\u7684\u4ee3\u7801\u548c\u6267\u884c\u6b21\u6570\u5373\u53ef\u3002\u793a\u4f8b\u4ee3\u7801\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;1000\u6b21\u6267\u884c\u7684\u5e73\u5747\u65f6\u95f4\u4e3a\uff1a{execution_time}\u79d2&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u9ad8\u7cbe\u5ea6\u8ba1\u65f6\uff1f<\/strong><br \/>\u5bf9\u4e8e\u9700\u8981\u9ad8\u7cbe\u5ea6\u8ba1\u65f6\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>time.perf_counter()<\/code>\u3002\u8fd9\u4e2a\u51fd\u6570\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5305\u542b\u7cfb\u7edf\u8fd0\u884c\u65f6\u95f4\u548c\u4f11\u7720\u65f6\u95f4\u7684\u6700\u51c6\u786e\u8ba1\u65f6\u5668\uff0c\u9002\u5408\u4e8e\u6027\u80fd\u5206\u6790\u3002\u4f7f\u7528\u65b9\u6cd5\u4e0e<code>time.time()<\/code>\u76f8\u4f3c\uff0c\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import time\n\nstart_time = time.perf_counter()\n# \u4f60\u8981\u8ba1\u65f6\u7684\u4ee3\u7801\nend_time = time.perf_counter()\n\nexecution_time = end_time - start_time\nprint(f&quot;\u9ad8\u7cbe\u5ea6\u8ba1\u65f6\u7ed3\u679c\uff1a{execution_time}\u79d2&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python\u8ba1\u65f6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528time\u6a21\u5757\u3001datetime\u6a21\u5757\u3001timeit\u6a21\u5757\u7b49\u3002time\u6a21\u5757\u63d0\u4f9b [&hellip;]","protected":false},"author":3,"featured_media":926561,"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\/926560"}],"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=926560"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/926560\/revisions"}],"predecessor-version":[{"id":926562,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/926560\/revisions\/926562"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/926561"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=926560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=926560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=926560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}