{"id":951896,"date":"2024-12-27T01:14:20","date_gmt":"2024-12-26T17:14:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/951896.html"},"modified":"2024-12-27T01:14:24","modified_gmt":"2024-12-26T17:14:24","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a1%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/951896.html","title":{"rendered":"python\u5982\u4f55\u8ba1\u65f6\u95f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085940\/b95f616c-a1a1-458a-936f-29ba12b7b863.webp\" alt=\"python\u5982\u4f55\u8ba1\u65f6\u95f4\" \/><\/p>\n<p><p> <strong>Python\u8ba1\u65f6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528time\u6a21\u5757\u3001datetime\u6a21\u5757\u3001\u4ee5\u53catimeit\u6a21\u5757\u3002<\/strong>\u5176\u4e2d\uff0ctime\u6a21\u5757\u9002\u5408\u7b80\u5355\u7684\u65f6\u95f4\u6d4b\u91cf\uff0cdatetime\u6a21\u5757\u63d0\u4f9b\u66f4\u591a\u65f6\u95f4\u5904\u7406\u529f\u80fd\uff0c\u800ctimeit\u6a21\u5757\u4e13\u4e3a\u6027\u80fd\u6d4b\u8bd5\u8bbe\u8ba1\u3002<strong>\u4f7f\u7528time\u6a21\u5757\u7684time()\u51fd\u6570\u6700\u4e3a\u7b80\u5355<\/strong>\uff0c\u5b83\u8fd4\u56de\u81ea\u7eaa\u5143\u4ee5\u6765\u7684\u79d2\u6570\uff0c\u53ef\u7528\u4e8e\u8ba1\u7b97\u4ee3\u7801\u8fd0\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001TIME\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>time<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7528\u4e8e\u5904\u7406\u65f6\u95f4\u7684\u51fd\u6570\u3002\u6700\u5e38\u7528\u7684\u662f<code>time()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\u3002\u901a\u8fc7\u8bb0\u5f55\u5f00\u59cb\u548c\u7ed3\u675f\u65f6\u95f4\u7684\u65f6\u95f4\u6233\uff0c\u53ef\u4ee5\u8ba1\u7b97\u4ee3\u7801\u7684\u8fd0\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528time()\u51fd\u6570<\/strong><\/li>\n<\/ul>\n<p><p>time()\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u8868\u793a\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\u3002\u901a\u8fc7\u5728\u4ee3\u7801\u6267\u884c\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u5904\u4f7f\u7528time()\uff0c\u53ef\u4ee5\u8ba1\u7b97\u51fa\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\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>print(&quot;\u8fd0\u884c\u65f6\u95f4\u4e3a\uff1a&quot;, end_time - start_time, &quot;\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528sleep()\u51fd\u6570<\/strong><\/li>\n<\/ul>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8ba9\u7a0b\u5e8f\u6682\u505c\u6267\u884c\u4e00\u6bb5\u65f6\u95f4\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528<code>time.sleep()<\/code>\u51fd\u6570\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u8868\u793a\u6682\u505c\u7684\u79d2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u5f00\u59cb&quot;)<\/p>\n<p>time.sleep(2)  # \u6682\u505c2\u79d2<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ed3\u675f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001DATETIME\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u529f\u80fd\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u8ba1\u7b97\u65f6\u95f4\u5dee\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/li>\n<\/ul>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>datetime.now()<\/code>\u6765\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>current_time = datetime.now()<\/p>\n<p>print(&quot;\u5f53\u524d\u65f6\u95f4\uff1a&quot;, current_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u8ba1\u7b97\u65f6\u95f4\u5dee<\/strong><\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u51cf\u53bb\u4e24\u4e2a<code>datetime<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a<code>timedelta<\/code>\u5bf9\u8c61\uff0c\u8868\u793a\u65f6\u95f4\u5dee\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>duration = end_time - start_time<\/p>\n<p>print(&quot;\u8fd0\u884c\u65f6\u95f4\u4e3a\uff1a&quot;, duration)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001TIMEIT\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>timeit<\/code>\u6a21\u5757\u4e13\u95e8\u7528\u4e8e\u6d4b\u91cf\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\uff0c\u5c24\u5176\u662f\u5c0f\u6bb5\u4ee3\u7801\u7684\u6027\u80fd\u6d4b\u8bd5\u3002\u5b83\u53ef\u4ee5\u81ea\u52a8\u6267\u884c\u6307\u5b9a\u6b21\u6570\u7684\u6d4b\u8bd5\uff0c\u5e76\u8fd4\u56de\u6700\u5c0f\u7684\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528timeit\u6a21\u5757<\/strong><\/li>\n<\/ul>\n<p><p>\u901a\u8fc7<code>timeit<\/code>\u6a21\u5757\u53ef\u4ee5\u66f4\u7cbe\u786e\u5730\u6d4b\u91cf\u4ee3\u7801\u6267\u884c\u65f6\u95f4\u3002\u5b83\u9002\u5408\u7528\u4e8e\u6027\u80fd\u4f18\u5316\u548c\u6bd4\u8f83\u4e0d\u540c\u5b9e\u73b0\u65b9\u5f0f\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>def test():<\/p>\n<p>    # \u4e00\u4e9b\u4ee3\u7801<\/p>\n<p>    pass<\/p>\n<p>execution_time = timeit.timeit(test, number=1000)<\/p>\n<p>print(&quot;\u6267\u884c\u65f6\u95f4\u4e3a\uff1a&quot;, execution_time, &quot;\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u547d\u4ee4\u884c<\/strong><\/li>\n<\/ul>\n<p><p><code>timeit<\/code>\u6a21\u5757\u4e5f\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u4f7f\u7528\uff0c\u8fd9\u5bf9\u4e8e\u5feb\u901f\u6d4b\u8bd5\u548c\u6bd4\u8f83\u4e0d\u540c\u4ee3\u7801\u7247\u6bb5\u7684\u6027\u80fd\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m timeit -s &quot;import module_name&quot; &quot;function_to_test()&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001PERF_COUNTER\u4e0ePROCESS_TIME<\/p>\n<\/p>\n<p><p>Python\u8fd8\u63d0\u4f9b\u4e86<code>perf_counter()<\/code>\u548c<code>process_time()<\/code>\u8fd9\u4e24\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u66f4\u7cbe\u786e\u7684\u8ba1\u65f6\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528perf_counter()<\/strong><\/li>\n<\/ul>\n<p><p><code>perf_counter()<\/code>\u8fd4\u56de\u4e00\u4e2a\u9ad8\u7cbe\u5ea6\u7684\u65f6\u95f4\u8ba1\u6570\u5668\uff0c\u9002\u5408\u7528\u4e8e\u6d4b\u91cf\u77ed\u65f6\u95f4\u95f4\u9694\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>print(&quot;\u8fd0\u884c\u65f6\u95f4\u4e3a\uff1a&quot;, end_time - start_time, &quot;\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528process_time()<\/strong><\/li>\n<\/ul>\n<p><p><code>process_time()<\/code>\u8fd4\u56de\u5f53\u524d\u8fdb\u7a0b\u7684CPU\u65f6\u95f4\uff0c\u4e0d\u5305\u62ec\u7761\u7720\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>start_time = time.process_time()<\/p>\n<h2><strong>\u6267\u884c\u4e00\u4e9b\u4ee3\u7801<\/strong><\/h2>\n<p>end_time = time.process_time()<\/p>\n<p>print(&quot;\u8fd0\u884c\u65f6\u95f4\u4e3a\uff1a&quot;, end_time - start_time, &quot;\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001APPLYING IN REAL-WORLD SCENARIOS<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u9700\u6c42\u7684\u4e0d\u540c\uff0c\u9009\u62e9\u5408\u9002\u7684\u8ba1\u65f6\u65b9\u6cd5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u6027\u80fd\u6d4b\u8bd5<\/strong><\/li>\n<\/ul>\n<p><p>\u5f53\u9700\u8981\u5bf9\u591a\u4e2a\u5b9e\u73b0\u8fdb\u884c\u6027\u80fd\u5bf9\u6bd4\u65f6\uff0c<code>timeit<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u5b83\u53ef\u4ee5\u81ea\u52a8\u591a\u6b21\u6267\u884c\u6d4b\u8bd5\uff0c\u786e\u4fdd\u7ed3\u679c\u7684\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1<\/strong><\/li>\n<\/ul>\n<p><p>\u5bf9\u4e8e\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\uff0c\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bb0\u5f55\u5f00\u59cb\u548c\u7ed3\u675f\u65f6\u95f4\uff0c\u5e76\u4e14\u80fd\u591f\u5f88\u5bb9\u6613\u5730\u8f93\u51fa\u683c\u5f0f\u5316\u7684\u65f6\u95f4\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u7cbe\u786e\u6d4b\u91cf<\/strong><\/li>\n<\/ul>\n<p><p>\u5728\u9700\u8981\u9ad8\u7cbe\u5ea6\u6d4b\u91cf\u7684\u573a\u666f\u4e2d\uff0c<code>perf_counter()<\/code>\u662f\u6700\u4f73\u9009\u62e9\uff0c\u5b83\u63d0\u4f9b\u4e86\u7eb3\u79d2\u7ea7\u522b\u7684\u7cbe\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528Python\u7684\u8fd9\u4e9b\u8ba1\u65f6\u529f\u80fd\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5206\u6790\u548c\u4f18\u5316\u4ee3\u7801\u7684\u6027\u80fd\uff0c\u63d0\u5347\u7a0b\u5e8f\u7684\u6548\u7387\u3002\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u5408\u7406\u7684\u65f6\u95f4\u7ba1\u7406\u4e5f\u662f\u786e\u4fdd\u9879\u76ee\u987a\u5229\u8fdb\u884c\u7684\u91cd\u8981\u4e00\u73af\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\u7684\u6267\u884c\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u4e2d\u7684<code>time()<\/code>\u51fd\u6570\u6765\u6d4b\u91cf\u4ee3\u7801\u5757\u7684\u6267\u884c\u65f6\u95f4\u3002\u9996\u5148\u8bb0\u5f55\u4e0b\u5f00\u59cb\u65f6\u95f4\uff0c\u7136\u540e\u6267\u884c\u4ee3\u7801\uff0c\u6700\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\u53ef\u4ee5\u5f97\u5230\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import time\n\nstart_time = time.time()\n# \u8fd9\u91cc\u653e\u5165\u8981\u6d4b\u91cf\u7684\u4ee3\u7801\nend_time = time.time()\nexecution_time = end_time - start_time\nprint(f&quot;\u4ee3\u7801\u6267\u884c\u65f6\u95f4: {execution_time}\u79d2&quot;)\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u6709\u5176\u4ed6\u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u6027\u80fd\u8ba1\u65f6\uff1f<\/strong><br \/>\u9664\u4e86<code>time<\/code>\u6a21\u5757\uff0cPython\u8fd8\u63d0\u4f9b\u4e86<code>timeit<\/code>\u6a21\u5757\uff0c\u5b83\u4e13\u95e8\u7528\u4e8e\u6d4b\u91cf\u5c0f\u6bb5\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\uff0c\u5c24\u5176\u9002\u5408\u6bd4\u8f83\u4e0d\u540c\u4ee3\u7801\u7684\u6027\u80fd\u3002\u4f7f\u7528<code>timeit<\/code>\u53ef\u4ee5\u907f\u514d\u591a\u6b21\u6267\u884c\u5e26\u6765\u7684\u8bef\u5dee\uff0c\u63d0\u4f9b\u66f4\u7cbe\u786e\u7684\u7ed3\u679c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import timeit\n\nexecution_time = timeit.timeit(&quot;your_code_here&quot;, number=1000)\nprint(f&quot;\u6267\u884c1000\u6b21\u7684\u5e73\u5747\u65f6\u95f4: {execution_time}\u79d2&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u9ad8\u7cbe\u5ea6\u7684\u65f6\u95f4\u6d4b\u91cf\uff1f<\/strong><br \/>\u5bf9\u4e8e\u9700\u8981\u9ad8\u7cbe\u5ea6\u6d4b\u91cf\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>time.perf_counter()<\/code>\u51fd\u6570\u3002\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5206\u8fa8\u7387\u7684\u8ba1\u65f6\u529f\u80fd\uff0c\u9002\u5408\u6d4b\u91cf\u77ed\u65f6\u95f4\u95f4\u9694\u7684\u4efb\u52a1\u3002\u4f7f\u7528\u65b9\u5f0f\u4e0e<code>time.time()<\/code>\u7c7b\u4f3c\uff1a  <\/p>\n<pre><code class=\"language-python\">import time\n\nstart_time = time.perf_counter()\n# \u8fd9\u91cc\u653e\u5165\u8981\u6d4b\u91cf\u7684\u4ee3\u7801\nend_time = time.perf_counter()\nexecution_time = end_time - start_time\nprint(f&quot;\u9ad8\u7cbe\u5ea6\u4ee3\u7801\u6267\u884c\u65f6\u95f4: {execution_time}\u79d2&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python\u8ba1\u65f6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528time\u6a21\u5757\u3001datetime\u6a21\u5757\u3001\u4ee5\u53catimeit\u6a21\u5757\u3002\u5176\u4e2d\uff0ctime\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":951901,"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\/951896"}],"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=951896"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/951896\/revisions"}],"predecessor-version":[{"id":951902,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/951896\/revisions\/951902"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/951901"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=951896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=951896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=951896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}