{"id":1073083,"date":"2025-01-08T11:22:03","date_gmt":"2025-01-08T03:22:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1073083.html"},"modified":"2025-01-08T11:22:06","modified_gmt":"2025-01-08T03:22:06","slug":"%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97python%e7%a8%8b%e5%ba%8f%e8%bf%90%e8%a1%8c%e5%a4%9a%e5%b0%91%e5%88%86%e9%92%9f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1073083.html","title":{"rendered":"\u5982\u4f55\u8ba1\u7b97python\u7a0b\u5e8f\u8fd0\u884c\u591a\u5c11\u5206\u949f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103109\/362dc87c-38c4-4a6f-b872-3133f91ce461.webp\" alt=\"\u5982\u4f55\u8ba1\u7b97python\u7a0b\u5e8f\u8fd0\u884c\u591a\u5c11\u5206\u949f\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u8ba1\u7b97python\u7a0b\u5e8f\u8fd0\u884c\u591a\u5c11\u5206\u949f<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528time\u6a21\u5757\u3001\u4f7f\u7528datetime\u6a21\u5757\u3001\u4f7f\u7528timeit\u6a21\u5757<\/strong>\uff0c\u662f\u8ba1\u7b97Python\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u7684\u4e09\u79cd\u4e3b\u8981\u65b9\u6cd5\u3002\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528time\u6a21\u5757\uff1a<\/strong> <code>time<\/code>\u6a21\u5757\u662fPython\u4e2d\u5e38\u7528\u7684\u6a21\u5757\u4e4b\u4e00\uff0c\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u65f6\u95f4\u76f8\u5173\u4efb\u52a1\u3002\u901a\u8fc7\u8be5\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233\uff0c\u5e76\u5728\u7a0b\u5e8f\u5f00\u59cb\u548c\u7ed3\u675f\u65f6\u5206\u522b\u8bb0\u5f55\u65f6\u95f4\u6233\u3002\u7136\u540e\uff0c\u901a\u8fc7\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u6233\u7684\u5dee\u503c\uff0c\u53ef\u4ee5\u5f97\u51fa\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u7684\u8be6\u7ec6\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>start_time = time.time()  # \u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/p>\n<h2><strong>\u4f60\u7684\u7a0b\u5e8f\u4ee3\u7801<\/strong><\/h2>\n<p>time.sleep(75)  # \u6a21\u62df\u7a0b\u5e8f\u8fd0\u884c75\u79d2<\/p>\n<p>end_time = time.time()  # \u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/p>\n<p>run_time = end_time - start_time  # \u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4<\/p>\n<p>run_time_minutes = run_time \/ 60  # \u5c06\u65f6\u95f4\u8f6c\u6362\u4e3a\u5206\u949f<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u4e3a {run_time_minutes} \u5206\u949f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u5728\u5f00\u59cb\u65f6\u8bb0\u5f55\u4e86\u5f00\u59cb\u65f6\u95f4\uff0c\u5728\u7ed3\u675f\u65f6\u8bb0\u5f55\u4e86\u7ed3\u675f\u65f6\u95f4\uff0c\u901a\u8fc7\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u6233\u7684\u5dee\u503c\uff0c\u5f97\u5230\u4e86\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5206\u949f\u8fdb\u884c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528time\u6a21\u5757<\/h3>\n<\/p>\n<p><p><strong>1. \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233<\/strong><\/p>\n<\/p>\n<p><p><code>time.time()<\/code>\u51fd\u6570\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\uff0c\u8fd9\u662f\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u8868\u793a\u4ece1970\u5e741\u67081\u65e5\uff08Unix\u7eaa\u5143\uff09\u5230\u73b0\u5728\u7684\u79d2\u6570\u3002\u53ef\u4ee5\u901a\u8fc7\u8be5\u65f6\u95f4\u6233\u6765\u8bb0\u5f55\u7a0b\u5e8f\u5f00\u59cb\u548c\u7ed3\u675f\u7684\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>start_time = time.time()  # \u8bb0\u5f55\u7a0b\u5e8f\u5f00\u59cb\u65f6\u95f4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u7a0b\u5e8f\u4ee3\u7801\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8bb0\u5f55\u4e86\u5f00\u59cb\u65f6\u95f4\u540e\uff0c\u53ef\u4ee5\u7f16\u5199\u6216\u8fd0\u884c\u4f60\u7684\u7a0b\u5e8f\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f60\u7684\u7a0b\u5e8f\u4ee3\u7801<\/p>\n<p>for i in range(1000000):<\/p>\n<p>    pass  # \u6a21\u62df\u7a0b\u5e8f\u8fd0\u884c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4\u5e76\u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u7a0b\u5e8f\u7ed3\u675f\u540e\uff0c\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4\uff0c\u5e76\u901a\u8fc7\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u6233\u7684\u5dee\u503c\u5f97\u5230\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">end_time = time.time()  # \u8bb0\u5f55\u7a0b\u5e8f\u7ed3\u675f\u65f6\u95f4<\/p>\n<p>run_time = end_time - start_time  # \u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4<\/p>\n<p>run_time_minutes = run_time \/ 60  # \u5c06\u65f6\u95f4\u8f6c\u6362\u4e3a\u5206\u949f<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u4e3a {run_time_minutes} \u5206\u949f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4. \u5b8c\u6574\u793a\u4f8b<\/strong><\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u8ba1\u7b97Python\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>start_time = time.time()  # \u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/p>\n<h2><strong>\u4f60\u7684\u7a0b\u5e8f\u4ee3\u7801<\/strong><\/h2>\n<p>for i in range(1000000):<\/p>\n<p>    pass  # \u6a21\u62df\u7a0b\u5e8f\u8fd0\u884c<\/p>\n<p>end_time = time.time()  # \u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/p>\n<p>run_time = end_time - start_time  # \u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4<\/p>\n<p>run_time_minutes = run_time \/ 60  # \u5c06\u65f6\u95f4\u8f6c\u6362\u4e3a\u5206\u949f<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u4e3a {run_time_minutes} \u5206\u949f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528datetime\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u65e5\u671f\u548c\u65f6\u95f4\u64cd\u4f5c\u529f\u80fd\uff0c\u76f8\u6bd4\u4e8e<code>time<\/code>\u6a21\u5757\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u3002\u901a\u8fc7<code>datetime<\/code>\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u5e76\u8fdb\u884c\u5dee\u503c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>datetime.now()<\/code>\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>start_time = datetime.now()  # \u8bb0\u5f55\u7a0b\u5e8f\u5f00\u59cb\u65f6\u95f4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u7a0b\u5e8f\u4ee3\u7801\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8bb0\u5f55\u4e86\u5f00\u59cb\u65f6\u95f4\u540e\uff0c\u53ef\u4ee5\u7f16\u5199\u6216\u8fd0\u884c\u4f60\u7684\u7a0b\u5e8f\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f60\u7684\u7a0b\u5e8f\u4ee3\u7801<\/p>\n<p>for i in range(1000000):<\/p>\n<p>    pass  # \u6a21\u62df\u7a0b\u5e8f\u8fd0\u884c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4\u5e76\u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u7a0b\u5e8f\u7ed3\u675f\u540e\uff0c\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4\uff0c\u5e76\u901a\u8fc7\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u7684\u5dee\u503c\u5f97\u5230\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">end_time = datetime.now()  # \u8bb0\u5f55\u7a0b\u5e8f\u7ed3\u675f\u65f6\u95f4<\/p>\n<p>run_time = end_time - start_time  # \u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4<\/p>\n<p>run_time_minutes = run_time.total_seconds() \/ 60  # \u5c06\u65f6\u95f4\u8f6c\u6362\u4e3a\u5206\u949f<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u4e3a {run_time_minutes} \u5206\u949f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4. \u5b8c\u6574\u793a\u4f8b<\/strong><\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u8ba1\u7b97Python\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>start_time = datetime.now()  # \u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/p>\n<h2><strong>\u4f60\u7684\u7a0b\u5e8f\u4ee3\u7801<\/strong><\/h2>\n<p>for i in range(1000000):<\/p>\n<p>    pass  # \u6a21\u62df\u7a0b\u5e8f\u8fd0\u884c<\/p>\n<p>end_time = datetime.now()  # \u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/p>\n<p>run_time = end_time - start_time  # \u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4<\/p>\n<p>run_time_minutes = run_time.total_seconds() \/ 60  # \u5c06\u65f6\u95f4\u8f6c\u6362\u4e3a\u5206\u949f<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u4e3a {run_time_minutes} \u5206\u949f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528timeit\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>timeit<\/code>\u6a21\u5757\u662fPython\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\u5904\u7406\u4e00\u4e9b\u5e38\u89c1\u7684\u8ba1\u65f6\u95ee\u9898\uff0c\u5982\u7f13\u5b58\u3001\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u7b49\u3002<code>timeit<\/code>\u6a21\u5757\u975e\u5e38\u9002\u5408\u7528\u6765\u6d4b\u91cf\u4ee3\u7801\u6bb5\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u4f7f\u7528timeit.timeit\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>timeit.timeit<\/code>\u51fd\u6570\u7528\u4e8e\u6d4b\u91cf\u4ee3\u7801\u6bb5\u7684\u6267\u884c\u65f6\u95f4\u3002\u8be5\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8981\u6d4b\u91cf\u7684\u4ee3\u7801\u5b57\u7b26\u4e32\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u8bbe\u7f6e\u4ee3\u7801\u6267\u884c\u73af\u5883\u7684\u5b57\u7b26\u4e32\uff08\u901a\u5e38\u7528\u4e8e\u5bfc\u5165\u6a21\u5757\u548c\u5b9a\u4e49\u51fd\u6570\uff09\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u91cd\u590d\u6267\u884c\u7684\u6b21\u6570\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>for i in range(1000000):<\/p>\n<p>    pass  # \u6a21\u62df\u7a0b\u5e8f\u8fd0\u884c<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>run_time = timeit.timeit(stmt=code_to_test, number=1)  # \u6d4b\u91cf\u4ee3\u7801\u6bb5\u7684\u6267\u884c\u65f6\u95f4<\/p>\n<p>run_time_minutes = run_time \/ 60  # \u5c06\u65f6\u95f4\u8f6c\u6362\u4e3a\u5206\u949f<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u4e3a {run_time_minutes} \u5206\u949f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u4f7f\u7528timeit.Timer\u7c7b<\/strong><\/p>\n<\/p>\n<p><p><code>timeit.Timer<\/code>\u7c7b\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u7528\u4e8e\u6d4b\u91cf\u66f4\u590d\u6742\u7684\u4ee3\u7801\u6bb5\u3002\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a<code>Timer<\/code>\u5bf9\u8c61\uff0c\u5e76\u8c03\u7528\u5176<code>timeit<\/code>\u65b9\u6cd5\u6765\u6d4b\u91cf\u4ee3\u7801\u6bb5\u7684\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>for i in range(1000000):<\/p>\n<p>    pass  # \u6a21\u62df\u7a0b\u5e8f\u8fd0\u884c<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>timer = timeit.Timer(stmt=code_to_test)<\/p>\n<p>run_time = timer.timeit(number=1)  # \u6d4b\u91cf\u4ee3\u7801\u6bb5\u7684\u6267\u884c\u65f6\u95f4<\/p>\n<p>run_time_minutes = run_time \/ 60  # \u5c06\u65f6\u95f4\u8f6c\u6362\u4e3a\u5206\u949f<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u4e3a {run_time_minutes} \u5206\u949f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u5b8c\u6574\u793a\u4f8b<\/strong><\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757\u8ba1\u7b97Python\u7a0b\u5e8f\u7684\u8fd0\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>for i in range(1000000):<\/p>\n<p>    pass  # \u6a21\u62df\u7a0b\u5e8f\u8fd0\u884c<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>run_time = timeit.timeit(stmt=code_to_test, number=1)  # \u6d4b\u91cf\u4ee3\u7801\u6bb5\u7684\u6267\u884c\u65f6\u95f4<\/p>\n<p>run_time_minutes = run_time \/ 60  # \u5c06\u65f6\u95f4\u8f6c\u6362\u4e3a\u5206\u949f<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u4e3a {run_time_minutes} \u5206\u949f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4e09\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u8ba1\u7b97Python\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5206\u949f\u8fdb\u884c\u663e\u793a\u3002\u6839\u636e\u4f60\u7684\u5177\u4f53\u9700\u6c42\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528<code>time<\/code>\u3001<code>datetime<\/code>\u6216<code>timeit<\/code>\u6a21\u5757\u6765\u8fdb\u884c\u65f6\u95f4\u6d4b\u91cf\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u4e86\u89e3\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u4f18\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6d4b\u91cf\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u6765\u6d4b\u91cf\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\u3002\u5177\u4f53\u65b9\u6cd5\u662f\u8bb0\u5f55\u7a0b\u5e8f\u5f00\u59cb\u8fd0\u884c\u7684\u65f6\u95f4\u548c\u7ed3\u675f\u65f6\u95f4\uff0c\u7136\u540e\u8ba1\u7b97\u5b83\u4eec\u7684\u5dee\u503c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import time\n\nstart_time = time.time()\n# \u8fd9\u91cc\u662f\u4f60\u7684\u7a0b\u5e8f\u4ee3\u7801\nend_time = time.time()\n\nexecution_time = end_time - start_time\nprint(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4: {execution_time}\u79d2&quot;)\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>time.time()<\/code>\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\uff0c\u8ba1\u7b97\u51fa\u7a0b\u5e8f\u8fd0\u884c\u7684\u603b\u65f6\u957f\u3002<\/p>\n<p><strong>\u6709\u4ec0\u4e48\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u8ba1\u7b97Python\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\u5417\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>time<\/code>\u6a21\u5757\uff0cPython\u8fd8\u63d0\u4f9b\u4e86<code>timeit<\/code>\u6a21\u5757\uff0c\u4e13\u95e8\u7528\u4e8e\u6d4b\u91cf\u5c0f\u6bb5\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\u3002\u5b83\u975e\u5e38\u9002\u5408\u7528\u4e8e\u6027\u80fd\u6d4b\u8bd5\u548c\u6bd4\u8f83\u4e0d\u540c\u4ee3\u7801\u5b9e\u73b0\u7684\u6548\u7387\u3002\u4f7f\u7528\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import timeit\n\nexecution_time = timeit.timeit(&#39;your_code_here&#39;, number=100)\nprint(f&quot;\u4ee3\u7801\u6267\u884c\u65f6\u95f4: {execution_time}\u79d2&quot;)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>your_code_here<\/code>\u662f\u9700\u8981\u6d4b\u8bd5\u7684\u4ee3\u7801\u5757\uff0c<code>number<\/code>\u53c2\u6570\u6307\u5b9a\u4ee3\u7801\u6267\u884c\u7684\u6b21\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u8f6c\u6362\u4e3a\u5206\u949f\uff1f<\/strong><br \/>\u8981\u5c06\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\u4ece\u79d2\u8f6c\u6362\u4e3a\u5206\u949f\uff0c\u53ea\u9700\u5c06\u79d2\u6570\u9664\u4ee560\u3002\u4f8b\u5982\uff0c\u5982\u679c\u7a0b\u5e8f\u8fd0\u884c\u4e86150\u79d2\uff0c\u53ef\u4ee5\u7528\u4ee5\u4e0b\u65b9\u5f0f\u8ba1\u7b97\u5206\u949f\u6570\uff1a  <\/p>\n<pre><code class=\"language-python\">minutes = execution_time \/ 60\nprint(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4: {minutes}\u5206\u949f&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u76f4\u89c2\u5730\u7406\u89e3\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u7279\u522b\u662f\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u8f83\u957f\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u8ba1\u7b97python\u7a0b\u5e8f\u8fd0\u884c\u591a\u5c11\u5206\u949f \u4f7f\u7528time\u6a21\u5757\u3001\u4f7f\u7528datetime\u6a21\u5757\u3001\u4f7f\u7528timeit\u6a21\u5757\uff0c\u662f\u8ba1\u7b97 [&hellip;]","protected":false},"author":3,"featured_media":1073093,"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\/1073083"}],"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=1073083"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073083\/revisions"}],"predecessor-version":[{"id":1073096,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073083\/revisions\/1073096"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1073093"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1073083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1073083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1073083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}