{"id":1029846,"date":"2024-12-31T11:11:56","date_gmt":"2024-12-31T03:11:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1029846.html"},"modified":"2024-12-31T11:11:58","modified_gmt":"2024-12-31T03:11:58","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%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1029846.html","title":{"rendered":"python\u5982\u4f55\u8ba1\u7b97\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/952645c0-4b08-4784-aa7b-e2664a96de8f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u8ba1\u7b97\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\" \/><\/p>\n<p><p> <strong>Python\u8ba1\u7b97\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u662f\u6700\u57fa\u7840\u548c\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u5927\u591a\u6570\u7684\u573a\u666f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u6765\u8ba1\u7b97\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u7684\u4e3b\u8981\u6b65\u9aa4\u662f\uff1a\u5728\u7a0b\u5e8f\u5f00\u59cb\u8fd0\u884c\u65f6\u8bb0\u5f55\u5f53\u524d\u65f6\u95f4\uff0c\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u518d\u6b21\u8bb0\u5f55\u5f53\u524d\u65f6\u95f4\uff0c\u8ba1\u7b97\u4e24\u8005\u7684\u65f6\u95f4\u5dee\u5373\u4e3a\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u3002\u793a\u4f8b\u5982\u4e0b\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>\u6a21\u62df\u7a0b\u5e8f\u8fd0\u884c<\/strong><\/h2>\n<p>for i in range(1000000):<\/p>\n<p>    pass<\/p>\n<p>end_time = time.time()  # \u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/p>\n<p>execution_time = end_time - start_time  # \u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\uff1a{execution_time}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u5408\u5927\u591a\u6570\u573a\u666f\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u6bd4\u8f83\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\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\u662fPython\u4e2d\u7528\u4e8e\u5904\u7406\u65f6\u95f4\u76f8\u5173\u64cd\u4f5c\u7684\u6807\u51c6\u6a21\u5757\u3002\u901a\u8fc7\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bb0\u5f55\u7a0b\u5e8f\u8fd0\u884c\u7684\u5f00\u59cb\u65f6\u95f4\u548c\u7ed3\u675f\u65f6\u95f4\uff0c\u4ece\u800c\u8ba1\u7b97\u51fa\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bb0\u5f55\u65f6\u95f4<\/h4>\n<\/p>\n<p><p><code>time<\/code>\u6a21\u5757\u4e2d\u6700\u5e38\u7528\u7684\u51fd\u6570\u662f<code>time.time()<\/code>\uff0c\u5b83\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\uff0c\u5355\u4f4d\u4e3a\u79d2\u3002\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u8bb0\u5f55\u7a0b\u5e8f\u5f00\u59cb\u548c\u7ed3\u675f\u7684\u65f6\u95f4\u70b9\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>\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c<\/strong><\/h2>\n<p>end_time = time.time()  # \u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/p>\n<p>execution_time = end_time - start_time  # \u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\uff1a{execution_time}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8ba1\u7b97\u65f6\u95f4<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8bb0\u5f55\u7a0b\u5e8f\u5f00\u59cb\u548c\u7ed3\u675f\u7684\u65f6\u95f4\u70b9\uff0c\u8ba1\u7b97\u4e24\u8005\u7684\u5dee\u503c\u5373\u4e3a\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\u3002\u4e0a\u9762\u7684\u793a\u4f8b\u5df2\u7ecf\u5c55\u793a\u4e86\u8fd9\u4e00\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u8ba1\u7b97\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u5408\u5927\u591a\u6570\u573a\u666f\u3002\u7f3a\u70b9\u662f\u5982\u679c\u9700\u8981\u8ba1\u7b97\u66f4\u9ad8\u7cbe\u5ea6\u7684\u65f6\u95f4\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u5176\u4ed6\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>timeit<\/code>\u6a21\u5757\u662fPython\u4e2d\u7684\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u6d4b\u91cf\u5c0f\u6bb5\u4ee3\u7801\u6267\u884c\u65f6\u95f4\u7684\u6a21\u5757\u3002\u5b83\u6bd4<code>time<\/code>\u6a21\u5757\u66f4\u7cbe\u786e\uff0c\u9002\u5408\u7528\u4e8e\u9700\u8981\u9ad8\u7cbe\u5ea6\u6d4b\u91cf\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>timeit<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>timeit.timeit()<\/code>\u51fd\u6570\u6765\u6d4b\u91cf\u4ee3\u7801\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>a = 10<\/p>\n<p>b = 20<\/p>\n<p>c = a + b<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>execution_time = timeit.timeit(code_to_test, number=1000000)<\/p>\n<p>print(f&quot;\u4ee3\u7801\u6267\u884c\u65f6\u95f4\uff1a{execution_time}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>timeit.timeit()<\/code>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u4ee3\u7801\u6bb5\uff0c\u4ee5\u53ca\u4e00\u4e2a<code>number<\/code>\u53c2\u6570\uff0c\u8868\u793a\u4ee3\u7801\u6bb5\u6267\u884c\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u9ad8\u7ea7\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>timeit<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>Timer<\/code>\u7c7b\uff0c\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u6d4b\u91cf\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>def test():<\/p>\n<p>    a = 10<\/p>\n<p>    b = 20<\/p>\n<p>    c = a + b<\/p>\n<p>timer = timeit.Timer(test)<\/p>\n<p>execution_time = timer.timeit(number=1000000)<\/p>\n<p>print(f&quot;\u4ee3\u7801\u6267\u884c\u65f6\u95f4\uff1a{execution_time}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757\u7684\u4f18\u70b9\u662f\u7cbe\u5ea6\u9ad8\uff0c\u9002\u5408\u6d4b\u91cf\u5c0f\u6bb5\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\u3002\u7f3a\u70b9\u662f\u4f7f\u7528\u8d77\u6765\u6bd4<code>time<\/code>\u6a21\u5757\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\uff0c\u4e0d\u9002\u5408\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u662fPython\u4e2d\u7528\u4e8e\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6807\u51c6\u6a21\u5757\u3002\u867d\u7136\u5b83\u4e3b\u8981\u7528\u4e8e\u65e5\u671f\u548c\u65f6\u95f4\u7684\u8ba1\u7b97\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u6d4b\u91cf\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bb0\u5f55\u65f6\u95f4<\/h4>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u4e2d\u6700\u5e38\u7528\u7684\u51fd\u6570\u662f<code>datetime.datetime.now()<\/code>\uff0c\u5b83\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u7684<code>datetime<\/code>\u5bf9\u8c61\u3002\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u8bb0\u5f55\u7a0b\u5e8f\u5f00\u59cb\u548c\u7ed3\u675f\u7684\u65f6\u95f4\u70b9\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>\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c<\/strong><\/h2>\n<p>end_time = datetime.now()  # \u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/p>\n<p>execution_time = (end_time - start_time).total_seconds()  # \u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\uff1a{execution_time}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8ba1\u7b97\u65f6\u95f4<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8bb0\u5f55\u7a0b\u5e8f\u5f00\u59cb\u548c\u7ed3\u675f\u7684\u65f6\u95f4\u70b9\uff0c\u8ba1\u7b97\u4e24\u8005\u7684\u5dee\u503c\uff0c\u5e76\u8f6c\u6362\u4e3a\u79d2\uff0c\u5373\u4e3a\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\u3002\u4e0a\u9762\u7684\u793a\u4f8b\u5df2\u7ecf\u5c55\u793a\u4e86\u8fd9\u4e00\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u8ba1\u7b97\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u9002\u5408\u9700\u8981\u540c\u65f6\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u573a\u666f\u3002\u7f3a\u70b9\u662f\u7cbe\u5ea6\u4e0d\u5982<code>timeit<\/code>\u6a21\u5757\u9ad8\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>perf_counter<\/code>\u548c<code>process_time<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python 3.3\u4ee5\u540e\u5f15\u5165\u4e86<code>perf_counter<\/code>\u548c<code>process_time<\/code>\u51fd\u6570\uff0c\u8fd9\u4e24\u4e2a\u51fd\u6570\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7cbe\u5ea6\u7684\u65f6\u95f4\u6d4b\u91cf\u3002<\/p>\n<\/p>\n<p><h4>1\u3001<code>perf_counter<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>perf_counter<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u9ad8\u7cbe\u5ea6\u7684\u65f6\u95f4\u6233\uff0c\u9002\u5408\u7528\u4e8e\u6d4b\u91cf\u77ed\u65f6\u95f4\u7684\u4ee3\u7801\u6bb5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>start_time = time.perf_counter()  # \u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/p>\n<h2><strong>\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c<\/strong><\/h2>\n<p>end_time = time.perf_counter()  # \u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/p>\n<p>execution_time = end_time - start_time  # \u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\uff1a{execution_time}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001<code>process_time<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>process_time<\/code>\u51fd\u6570\u8fd4\u56de\u5f53\u524d\u8fdb\u7a0b\u7684CPU\u65f6\u95f4\uff0c\u9002\u5408\u7528\u4e8e\u6d4b\u91cf\u7a0b\u5e8f\u5360\u7528\u7684CPU\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>start_time = time.process_time()  # \u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/p>\n<h2><strong>\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c<\/strong><\/h2>\n<p>end_time = time.process_time()  # \u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/p>\n<p>execution_time = end_time - start_time  # \u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\uff1a{execution_time}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>perf_counter<\/code>\u548c<code>process_time<\/code>\u51fd\u6570\u7684\u4f18\u70b9\u662f\u7cbe\u5ea6\u9ad8\uff0c\u9002\u5408\u6d4b\u91cf\u77ed\u65f6\u95f4\u7684\u4ee3\u7801\u6bb5\u3002\u7f3a\u70b9\u662f\u4f7f\u7528\u8d77\u6765\u6bd4<code>time<\/code>\u6a21\u5757\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0cPython\u4e2d\u8ba1\u7b97\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u7528\u7684\u5305\u62ec\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u3001<code>timeit<\/code>\u6a21\u5757\u3001<code>datetime<\/code>\u6a21\u5757\u4ee5\u53ca<code>perf_counter<\/code>\u548c<code>process_time<\/code>\u51fd\u6570\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u822c\u7684\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u6765\u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4\uff1b\u5bf9\u4e8e\u9700\u8981\u9ad8\u7cbe\u5ea6\u6d4b\u91cf\u7684\u4ee3\u7801\u6bb5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757\u6216<code>perf_counter<\/code>\u51fd\u6570\uff1b\u5bf9\u4e8e\u9700\u8981\u540c\u65f6\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\uff1b\u5bf9\u4e8e\u9700\u8981\u6d4b\u91cf\u7a0b\u5e8f\u5360\u7528\u7684CPU\u65f6\u95f4\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>process_time<\/code>\u51fd\u6570\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 \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u4e2d\u7684<code>time()<\/code>\u51fd\u6570\u6765\u6d4b\u91cf\u7279\u5b9a\u4ee3\u7801\u5757\u7684\u6267\u884c\u65f6\u95f4\u3002\u901a\u8fc7\u8bb0\u5f55\u4ee3\u7801\u6267\u884c\u524d\u540e\u7684\u65f6\u95f4\u6233\uff0c\u8ba1\u7b97\u4e24\u8005\u7684\u5dee\u503c\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import time\n\nstart_time = time.time()\n# \u8fd9\u91cc\u653e\u7f6e\u8981\u6d4b\u91cf\u7684\u4ee3\u7801\nend_time = time.time()\nexecution_time = end_time - start_time\nprint(f&quot;\u6267\u884c\u65f6\u95f4: {execution_time}\u79d2&quot;)\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528\u4ec0\u4e48\u5de5\u5177\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u6d4b\u91cfPython\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\uff1f<\/strong><br \/>\u9664\u4e86\u624b\u52a8\u8bb0\u5f55\u65f6\u95f4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757\u6765\u66f4\u7cbe\u786e\u5730\u6d4b\u91cf\u5c0f\u6bb5\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\u3002<code>timeit<\/code>\u4f1a\u81ea\u52a8\u6267\u884c\u591a\u6b21\u4ee5\u83b7\u5f97\u66f4\u51c6\u786e\u7684\u5e73\u5747\u503c\uff0c\u975e\u5e38\u9002\u5408\u6027\u80fd\u6d4b\u8bd5\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;\u5e73\u5747\u6267\u884c\u65f6\u95f4: {execution_time}\u79d2&quot;)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u67e5\u770b\u51fd\u6570\u7684\u8fd0\u884c\u65f6\u95f4\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u60f3\u8981\u6d4b\u91cf\u7279\u5b9a\u51fd\u6570\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528<code>time<\/code>\u6a21\u5757\uff0c\u6216\u8005\u4f7f\u7528\u88c5\u9970\u5668\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import time\n\ndef time_it(func):\n    def wrapper(*args, **kwargs):\n        start_time = time.time()\n        result = func(*args, **kwargs)\n        end_time = time.time()\n        print(f&quot;{func.__name__}\u6267\u884c\u65f6\u95f4: {end_time - start_time}\u79d2&quot;)\n        return result\n    return wrapper\n\n@time_it\ndef your_function():\n    # \u8fd9\u91cc\u653e\u7f6e\u51fd\u6570\u7684\u4ee3\u7801\n    pass\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u8ba9\u60a8\u8f7b\u677e\u5730\u8ddf\u8e2a\u591a\u4e2a\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8ba1\u7b97\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528time\u6a21\u5757\u3001\u4f7f\u7528timeit\u6a21\u5757\u3001\u4f7f\u7528datetime\u6a21\u5757\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1029856,"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\/1029846"}],"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=1029846"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1029846\/revisions"}],"predecessor-version":[{"id":1029857,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1029846\/revisions\/1029857"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1029856"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1029846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1029846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1029846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}