{"id":1095930,"date":"2025-01-08T14:56:09","date_gmt":"2025-01-08T06:56:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1095930.html"},"modified":"2025-01-08T14:56:12","modified_gmt":"2025-01-08T06:56:12","slug":"%e5%a6%82%e4%bd%95%e6%b1%82%e7%a8%8b%e5%ba%8f%e8%bf%90%e8%a1%8c%e6%97%b6%e9%97%b4python-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1095930.html","title":{"rendered":"\u5982\u4f55\u6c42\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211346\/0b33f01a-56ab-47e6-80e4-ed7b8b94311c.webp\" alt=\"\u5982\u4f55\u6c42\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4python\" \/><\/p>\n<p><p> <strong>\u8981\u83b7\u53d6\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u6bd4\u5982\u4f7f\u7528time\u6a21\u5757\u3001\u4f7f\u7528datetime\u6a21\u5757\u3001\u4f7f\u7528timeit\u6a21\u5757\u3002<\/strong> \u5176\u4e2d\uff0c<strong>time\u6a21\u5757\u6700\u7b80\u5355\uff0ctimeit\u6a21\u5757\u6700\u51c6\u786e\uff0cdatetime\u6a21\u5757\u9002\u5408\u4eba\u7c7b\u53ef\u8bfb\u683c\u5f0f\u3002<\/strong> \u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528\u5b83\u4eec\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u9010\u4e00\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u52a0\u6df1\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001TIME\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528time\u6a21\u5757\u6d4b\u91cf\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u975e\u5e38\u7b80\u5355\u3002\u6211\u4eec\u53ea\u9700\u8981\u5728\u7a0b\u5e8f\u5f00\u59cb\u548c\u7ed3\u675f\u65f6\u8bb0\u5f55\u65f6\u95f4\u6233\uff0c\u7136\u540e\u8ba1\u7b97\u4e24\u8005\u7684\u5dee\u503c\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528time\u6a21\u5757\u7684\u6b65\u9aa4\uff1a<\/h4>\n<\/p>\n<ol>\n<li>\u5bfc\u5165time\u6a21\u5757\u3002<\/li>\n<li>\u5728\u7a0b\u5e8f\u5f00\u59cb\u548c\u7ed3\u675f\u5904\u5206\u522b\u8c03\u7528<code>time.time()<\/code>\u51fd\u6570\u8bb0\u5f55\u65f6\u95f4\u6233\u3002<\/li>\n<li>\u8ba1\u7b97\u5e76\u6253\u5370\u65f6\u95f4\u5dee\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<h2><strong>\u4f60\u7684\u7a0b\u5e8f\u4ee3\u7801<\/strong><\/h2>\n<p>for i in range(1000000):<\/p>\n<p>    pass<\/p>\n<h2><strong>\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/strong><\/h2>\n<p>end_time = time.time()<\/p>\n<h2><strong>\u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4<\/strong><\/h2>\n<p>execution_time = end_time - start_time<\/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><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u5feb\u901f\u6d4b\u91cf\u7a0b\u5e8f\u7247\u6bb5\u7684\u8fd0\u884c\u65f6\u95f4\u3002<\/li>\n<li>\u9002\u7528\u4e8e\u5927\u591a\u6570\u7b80\u5355\u7684\u5e94\u7528\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u6d4b\u91cf\u975e\u5e38\u77ed\u7684\u65f6\u95f4\u95f4\u9694\uff0c\u53ef\u80fd\u4e0d\u591f\u51c6\u786e\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001DATETIME\u6a21\u5757<\/h3>\n<\/p>\n<p><p>datetime\u6a21\u5757\u9002\u7528\u4e8e\u9700\u8981\u4eba\u7c7b\u53ef\u8bfb\u683c\u5f0f\u7684\u65f6\u95f4\u6d4b\u91cf\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>datetime.datetime.now()<\/code>\u51fd\u6570\u6765\u8bb0\u5f55\u65f6\u95f4\u70b9\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528datetime\u6a21\u5757\u7684\u6b65\u9aa4\uff1a<\/h4>\n<\/p>\n<ol>\n<li>\u5bfc\u5165datetime\u6a21\u5757\u3002<\/li>\n<li>\u5728\u7a0b\u5e8f\u5f00\u59cb\u548c\u7ed3\u675f\u5904\u5206\u522b\u8c03\u7528<code>datetime.datetime.now()<\/code>\u51fd\u6570\u8bb0\u5f55\u65f6\u95f4\u70b9\u3002<\/li>\n<li>\u8ba1\u7b97\u5e76\u6253\u5370\u65f6\u95f4\u5dee\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/strong><\/h2>\n<p>start_time = datetime.now()<\/p>\n<h2><strong>\u4f60\u7684\u7a0b\u5e8f\u4ee3\u7801<\/strong><\/h2>\n<p>for i in range(1000000):<\/p>\n<p>    pass<\/p>\n<h2><strong>\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/strong><\/h2>\n<p>end_time = datetime.now()<\/p>\n<h2><strong>\u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4<\/strong><\/h2>\n<p>execution_time = end_time - start_time<\/p>\n<p>print(f&quot;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\uff1a{execution_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u8f93\u51fa\u7ed3\u679c\u4e3a\u4eba\u7c7b\u53ef\u8bfb\u7684\u683c\u5f0f\uff0c\u5305\u542b\u5929\u3001\u5c0f\u65f6\u3001\u5206\u949f\u3001\u79d2\u7b49\u4fe1\u606f\u3002<\/li>\n<li>\u9002\u7528\u4e8e\u9700\u8981\u8be6\u7ec6\u65f6\u95f4\u4fe1\u606f\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7a0b\u5e8f\u4ee3\u7801\u7565\u663e\u5197\u957f\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001TIMEIT\u6a21\u5757<\/h3>\n<\/p>\n<p><p>timeit\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u4e13\u95e8\u7528\u4e8e\u6d4b\u91cf\u5c0f\u4ee3\u7801\u6bb5\u6267\u884c\u65f6\u95f4\u7684\u6a21\u5757\uff0c\u5177\u6709\u9ad8\u7cbe\u5ea6\u548c\u53ef\u9760\u6027\u3002\u5b83\u53ef\u4ee5\u81ea\u52a8\u5904\u7406\u4ee3\u7801\u8fd0\u884c\u65f6\u7684\u5404\u79cd\u7ec6\u5fae\u5dee\u522b\uff0c\u63a8\u8350\u7528\u4e8e\u6027\u80fd\u5206\u6790\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528timeit\u6a21\u5757\u7684\u6b65\u9aa4\uff1a<\/h4>\n<\/p>\n<ol>\n<li>\u5bfc\u5165timeit\u6a21\u5757\u3002<\/li>\n<li>\u4f7f\u7528<code>timeit.timeit()<\/code>\u51fd\u6570\u6d4b\u91cf\u4ee3\u7801\u6bb5\u7684\u8fd0\u884c\u65f6\u95f4\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<h2><strong>\u4ee3\u7801\u6bb5<\/strong><\/h2>\n<p>code_to_test = &quot;&quot;&quot;<\/p>\n<p>for i in range(1000000):<\/p>\n<p>    pass<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<h2><strong>\u8bb0\u5f55\u8fd0\u884c\u65f6\u95f4<\/strong><\/h2>\n<p>execution_time = timeit.timeit(code_to_test, number=1)<\/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><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9ad8\u7cbe\u5ea6\uff0c\u9002\u7528\u4e8e\u6d4b\u91cf\u975e\u5e38\u77ed\u7684\u65f6\u95f4\u95f4\u9694\u3002<\/li>\n<li>\u81ea\u52a8\u5904\u7406\u4ee3\u7801\u6267\u884c\u7684\u7ec6\u5fae\u5dee\u522b\uff0c\u7ed3\u679c\u66f4\u53ef\u9760\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u7a0d\u663e\u590d\u6742\uff0c\u9002\u5408\u6709\u4e00\u5b9a\u7f16\u7a0b\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u9009\u62e9<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u573a\u666f\u53ca\u63a8\u8350\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5feb\u901f\u6d4b\u8bd5\u4ee3\u7801\u7247\u6bb5<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u5feb\u901f\u6d4b\u8bd5\u4ee3\u7801\u7247\u6bb5\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528time\u6a21\u5757\u3002\u5b83\u7b80\u5355\u6613\u7528\uff0c\u80fd\u591f\u5feb\u901f\u5f97\u5230\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>2. \u4eba\u7c7b\u53ef\u8bfb\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u8bb0\u5f55\u65f6\u95f4\u65e5\u5fd7\uff0c\u6216\u8005\u5e0c\u671b\u4ee5\u4eba\u7c7b\u53ef\u8bfb\u7684\u683c\u5f0f\u8f93\u51fa\u65f6\u95f4\u4fe1\u606f\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528datetime\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684\u65f6\u95f4\u4fe1\u606f\uff0c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h4>3. \u9ad8\u7cbe\u5ea6\u6d4b\u91cf<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u9ad8\u7cbe\u5ea6\u6d4b\u91cf\u7684\u573a\u666f\uff0c\u7279\u522b\u662f\u8fdb\u884c\u6027\u80fd\u5206\u6790\u548c\u4f18\u5316\u65f6\uff0c\u63a8\u8350\u4f7f\u7528timeit\u6a21\u5757\u3002\u5b83\u80fd\u591f\u63d0\u4f9b\u66f4\u4e3a\u51c6\u786e\u7684\u6d4b\u91cf\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u4e0a\u8ff0\u4e09\u79cd\u65b9\u6cd5\u6d4b\u91cf\u540c\u4e00\u6bb5\u4ee3\u7801\u7684\u8fd0\u884c\u65f6\u95f4\u3002\u901a\u8fc7\u5bf9\u6bd4\u4e0d\u540c\u65b9\u6cd5\u7684\u8f93\u51fa\u7ed3\u679c\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u5b83\u4eec\u7684\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>from datetime import datetime<\/p>\n<p>import timeit<\/p>\n<h2><strong>\u5b9a\u4e49\u6d4b\u8bd5\u4ee3\u7801<\/strong><\/h2>\n<p>def test_code():<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        pass<\/p>\n<h2><strong>\u4f7f\u7528time\u6a21\u5757<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>test_code()<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;time\u6a21\u5757\u6d4b\u91cf\u65f6\u95f4\uff1a{end_time - start_time} \u79d2&quot;)<\/p>\n<h2><strong>\u4f7f\u7528datetime\u6a21\u5757<\/strong><\/h2>\n<p>start_time = datetime.now()<\/p>\n<p>test_code()<\/p>\n<p>end_time = datetime.now()<\/p>\n<p>print(f&quot;datetime\u6a21\u5757\u6d4b\u91cf\u65f6\u95f4\uff1a{end_time - start_time}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528timeit\u6a21\u5757<\/strong><\/h2>\n<p>execution_time = timeit.timeit(&quot;test_code()&quot;, setup=&quot;from __m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__ import test_code&quot;, number=1)<\/p>\n<p>print(f&quot;timeit\u6a21\u5757\u6d4b\u91cf\u65f6\u95f4\uff1a{execution_time} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e0d\u540c\u65b9\u6cd5\u7684\u6d4b\u91cf\u7ed3\u679c\uff0c\u5e76\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u6d4b\u91cf\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u662f\u6027\u80fd\u4f18\u5316\u548c\u5206\u6790\u7684\u91cd\u8981\u6b65\u9aa4\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff0c\u5305\u62ectime\u6a21\u5757\u3001datetime\u6a21\u5757\u548ctimeit\u6a21\u5757\u3002<strong>time\u6a21\u5757\u7b80\u5355\u6613\u7528\uff0cdatetime\u6a21\u5757\u9002\u5408\u4eba\u7c7b\u53ef\u8bfb\u683c\u5f0f\uff0ctimeit\u6a21\u5757\u9ad8\u7cbe\u5ea6\u4e14\u9002\u7528\u4e8e\u6027\u80fd\u5206\u6790\u3002<\/strong> \u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u51c6\u786e\u5730\u8bc4\u4f30\u7a0b\u5e8f\u6027\u80fd\u5e76\u8fdb\u884c\u4f18\u5316\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u60a8\u80fd\u5bf9\u8fd9\u4e9b\u65b9\u6cd5\u6709\u66f4\u6df1\u5165\u7684\u7406\u89e3\uff0c\u5e76\u80fd\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u8fd0\u7528\u3002<\/p>\n<\/p>\n<p><p>\u5728\u63a5\u4e0b\u6765\u7684\u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u63a2\u8ba8\u4e00\u4e9b\u9ad8\u7ea7\u5e94\u7528\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u5e2e\u52a9\u60a8\u5728\u590d\u6742\u9879\u76ee\u4e2d\u66f4\u597d\u5730\u6d4b\u91cf\u548c\u4f18\u5316\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u590d\u6742\u7684\u573a\u666f\uff0c\u9700\u8981\u66f4\u7075\u6d3b\u548c\u9ad8\u7ea7\u7684\u65f6\u95f4\u6d4b\u91cf\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u9ad8\u7ea7\u5e94\u7528\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1. \u6d4b\u91cf\u51fd\u6570\u8fd0\u884c\u65f6\u95f4\u7684\u88c5\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u9ad8\u7ea7\u7684Python\u7279\u6027\uff0c\u53ef\u4ee5\u7528\u6765\u589e\u5f3a\u51fd\u6570\u7684\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u7528\u4e8e\u6d4b\u91cf\u4efb\u610f\u51fd\u6570\u7684\u8fd0\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def measure_time(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        start_time = time.time()<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        end_time = time.time()<\/p>\n<p>        print(f&quot;{func.__name__} \u8fd0\u884c\u65f6\u95f4\uff1a{end_time - start_time} \u79d2&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@measure_time<\/p>\n<p>def test_function():<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        pass<\/p>\n<p>test_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e2a\u88c5\u9970\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u6d4b\u91cf\u4efb\u610f\u51fd\u6570\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u800c\u4e0d\u9700\u8981\u5728\u6bcf\u4e2a\u51fd\u6570\u4e2d\u6dfb\u52a0\u91cd\u590d\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>2. \u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u7684\u65f6\u95f4\u6d4b\u91cf<\/h4>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u73af\u5883\u4e2d\uff0c\u6d4b\u91cf\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u53ef\u80fd\u4f1a\u66f4\u52a0\u590d\u6742\u3002\u6211\u4eec\u9700\u8981\u786e\u4fdd\u65f6\u95f4\u6d4b\u91cf\u7684\u51c6\u786e\u6027\uff0c\u5e76\u8003\u8651\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u7684\u542f\u52a8\u548c\u8c03\u5ea6\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h5>\u591a\u7ebf\u7a0b\u793a\u4f8b\uff1a<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import threading<\/p>\n<p>def measure_time(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        start_time = time.time()<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        end_time = time.time()<\/p>\n<p>        print(f&quot;{func.__name__} \u8fd0\u884c\u65f6\u95f4\uff1a{end_time - start_time} \u79d2&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@measure_time<\/p>\n<p>def test_function():<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        pass<\/p>\n<p>threads = []<\/p>\n<p>for _ in range(4):<\/p>\n<p>    thread = threading.Thread(target=test_function)<\/p>\n<p>    threads.append(thread)<\/p>\n<p>start_time = time.time()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;\u603b\u8fd0\u884c\u65f6\u95f4\uff1a{end_time - start_time} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u591a\u8fdb\u7a0b\u793a\u4f8b\uff1a<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import multiprocessing<\/p>\n<p>def measure_time(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        start_time = time.time()<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        end_time = time.time()<\/p>\n<p>        print(f&quot;{func.__name__} \u8fd0\u884c\u65f6\u95f4\uff1a{end_time - start_time} \u79d2&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@measure_time<\/p>\n<p>def test_function():<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        pass<\/p>\n<p>processes = []<\/p>\n<p>for _ in range(4):<\/p>\n<p>    process = multiprocessing.Process(target=test_function)<\/p>\n<p>    processes.append(process)<\/p>\n<p>start_time = time.time()<\/p>\n<p>for process in processes:<\/p>\n<p>    process.start()<\/p>\n<p>for process in processes:<\/p>\n<p>    process.join()<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;\u603b\u8fd0\u884c\u65f6\u95f4\uff1a{end_time - start_time} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u6d4b\u91cf\u548c\u4f18\u5316\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662f\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728\u4ee3\u7801\u5757\u6267\u884c\u524d\u540e\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u7528\u4e8e\u6d4b\u91cf\u4ee3\u7801\u5757\u7684\u8fd0\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>class MeasureTime:<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        self.start_time = time.time()<\/p>\n<p>        return self<\/p>\n<p>    def __exit__(self, exc_type, exc_val, exc_tb):<\/p>\n<p>        self.end_time = time.time()<\/p>\n<p>        print(f&quot;\u4ee3\u7801\u5757\u8fd0\u884c\u65f6\u95f4\uff1a{self.end_time - self.start_time} \u79d2&quot;)<\/p>\n<p>with MeasureTime():<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5b9a\u671f\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u5b9a\u671f\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u53ca\u65f6\u53d1\u73b0\u6027\u80fd\u74f6\u9888\u5e76\u8fdb\u884c\u4f18\u5316\u3002\u53ef\u4ee5\u4f7f\u7528\u81ea\u52a8\u5316\u6d4b\u8bd5\u5de5\u5177\u7ed3\u5408\u65f6\u95f4\u6d4b\u91cf\u65b9\u6cd5\uff0c\u7f16\u5199\u6027\u80fd\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>3. \u8bb0\u5f55\u548c\u5206\u6790\u65f6\u95f4\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5c06\u65f6\u95f4\u6d4b\u91cf\u6570\u636e\u8bb0\u5f55\u4e0b\u6765\uff0c\u5e76\u8fdb\u884c\u5206\u6790\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u7a0b\u5e8f\u7684\u6027\u80fd\u8868\u73b0\u3002\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u65e5\u5fd7\u7cfb\u7edf\u6216\u6027\u80fd\u76d1\u63a7\u5de5\u5177\uff0c\u5bf9\u65f6\u95f4\u6570\u636e\u8fdb\u884c\u7cfb\u7edf\u5316\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u6d4b\u91cf\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u662f\u6027\u80fd\u4f18\u5316\u548c\u5206\u6790\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u901a\u8fc7\u5b66\u4e60\u548c\u638c\u63e1time\u6a21\u5757\u3001datetime\u6a21\u5757\u548ctimeit\u6a21\u5757\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u65f6\u95f4\u6d4b\u91cf\u3002\u9ad8\u7ea7\u5e94\u7528\u548c\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u590d\u6742\u9879\u76ee\u4e2d\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u60a8\u80fd\u5bf9\u8fd9\u4e9b\u65b9\u6cd5\u6709\u66f4\u6df1\u5165\u7684\u7406\u89e3\uff0c\u5e76\u80fd\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u8fd0\u7528\u3002\u6d4b\u91cf\u548c\u4f18\u5316\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u662f\u4e00\u9879\u957f\u671f\u7684\u5de5\u4f5c\uff0c\u901a\u8fc7\u4e0d\u65ad\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u6211\u4eec\u53ef\u4ee5\u4e0d\u65ad\u63d0\u5347\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\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\u8fd0\u884c\u65f6\u95f4\uff1f<\/strong><br \/>\u8981\u6d4b\u91cf\u7279\u5b9a\u4ee3\u7801\u5757\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>time<\/code>\u6a21\u5757\u3002\u901a\u8fc7\u5728\u4ee3\u7801\u5757\u524d\u540e\u8c03\u7528<code>time.time()<\/code>\uff0c\u60a8\u53ef\u4ee5\u8ba1\u7b97\u51fa\u6267\u884c\u6240\u9700\u7684\u65f6\u95f4\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import time\n\nstart_time = time.time()\n# \u9700\u8981\u6d4b\u91cf\u7684\u4ee3\u7801\nend_time = time.time()\nprint(f&quot;\u8fd0\u884c\u65f6\u95f4: {end_time - start_time} \u79d2&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u7b80\u5355\u7684\u65f6\u95f4\u6d4b\u91cf\uff0c\u9002\u7528\u4e8e\u77ed\u5c0f\u7684\u4ee3\u7801\u7247\u6bb5\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u66f4\u7cbe\u786e\u7684\u65b9\u5f0f\u6765\u6d4b\u91cfPython\u7a0b\u5e8f\u7684\u6267\u884c\u65f6\u95f4\uff1f<\/strong><br \/>\u786e\u5b9e\u53ef\u4ee5\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757\uff0c\u5b83\u4e13\u95e8\u7528\u4e8e\u6d4b\u91cf\u5c0f\u6bb5\u4ee3\u7801\u7684\u8fd0\u884c\u65f6\u95f4\u3002\u901a\u8fc7\u591a\u6b21\u8fd0\u884c\u4ee3\u7801\u5e76\u53d6\u5e73\u5747\u503c\uff0c<code>timeit<\/code>\u63d0\u4f9b\u4e86\u66f4\u7cbe\u786e\u7684\u7ed3\u679c\u3002\u4f7f\u7528\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import timeit\n\nexecution_time = timeit.timeit(&#39;your_function()&#39;, setup=&#39;from __main__ import your_function&#39;, number=1000)\nprint(f&quot;\u5e73\u5747\u8fd0\u884c\u65f6\u95f4: {execution_time \/ 1000} \u79d2&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u7279\u522b\u9002\u5408\u6bd4\u8f83\u591a\u4e2a\u5b9e\u73b0\u7684\u6027\u80fd\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5927\u578bPython\u9879\u76ee\u4e2d\u76d1\u63a7\u6574\u4f53\u8fd0\u884c\u65f6\u95f4\uff1f<\/strong><br \/>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u7ed3\u5408\u65f6\u95f4\u6d4b\u91cf\uff0c\u6765\u8bb0\u5f55\u6574\u4e2a\u7a0b\u5e8f\u6216\u7279\u5b9a\u6a21\u5757\u7684\u8fd0\u884c\u65f6\u95f4\u3002\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u76d1\u63a7\u4efb\u4f55\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\uff1a  <\/p>\n<pre><code class=\"language-python\">import time\nimport logging\n\nlogging.basicConfig(level=logging.INFO)\n\ndef time_logger(func):\n    def wrapper(*args, **kwargs):\n        start_time = time.time()\n        result = func(*args, **kwargs)\n        end_time = time.time()\n        logging.info(f&quot;{func.__name__} \u6267\u884c\u65f6\u95f4: {end_time - start_time} \u79d2&quot;)\n        return result\n    return wrapper\n\n@time_logger\ndef your_function():\n    # \u4ee3\u7801\u903b\u8f91\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5728\u9879\u76ee\u4e2d\u5feb\u901f\u5b9a\u4f4d\u6027\u80fd\u74f6\u9888\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u83b7\u53d6\u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u6bd4\u5982\u4f7f\u7528time\u6a21\u5757\u3001\u4f7f\u7528datetime\u6a21\u5757\u3001\u4f7f\u7528timeit\u6a21\u5757\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1095940,"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\/1095930"}],"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=1095930"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095930\/revisions"}],"predecessor-version":[{"id":1095944,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095930\/revisions\/1095944"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1095940"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1095930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1095930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1095930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}