{"id":1037009,"date":"2024-12-31T12:11:37","date_gmt":"2024-12-31T04:11:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1037009.html"},"modified":"2024-12-31T12:11:40","modified_gmt":"2024-12-31T04:11:40","slug":"%e5%a6%82%e4%bd%95%e6%a3%80%e6%b5%8bpython-%e4%bb%a3%e7%a0%81%e8%bf%90%e8%a1%8c%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1037009.html","title":{"rendered":"\u5982\u4f55\u68c0\u6d4bpython \u4ee3\u7801\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\/7a371daf-b303-41c1-8255-0c3e16866d9d.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u68c0\u6d4bpython \u4ee3\u7801\u8fd0\u884c\u65f6\u95f4\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u68c0\u6d4bPython\u4ee3\u7801\u8fd0\u884c\u65f6\u95f4\uff1a\u4f7f\u7528time\u6a21\u5757\u3001\u4f7f\u7528timeit\u6a21\u5757\u3001\u4f7f\u7528datetime\u6a21\u5757\u3001\u5206\u6790\u4ee3\u7801\u7684\u74f6\u9888<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u662f\u68c0\u6d4bPython\u4ee3\u7801\u8fd0\u884c\u65f6\u95f4\u7684\u5e38\u89c1\u65b9\u6cd5\u3002\u901a\u8fc7\u5728\u4ee3\u7801\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u5904\u83b7\u53d6\u65f6\u95f4\u6233\uff0c\u5e76\u8ba1\u7b97\u5b83\u4eec\u4e4b\u95f4\u7684\u5dee\u503c\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u5f97\u4ee3\u7801\u5757\u7684\u8fd0\u884c\u65f6\u95f4\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u68c0\u6d4bPython\u4ee3\u7801\u8fd0\u884c\u65f6\u95f4\u7684\u65b9\u6cd5\uff0c\u5e76\u63a2\u8ba8\u5176\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528time\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u8bb0\u5f55\u4ee3\u7801\u8fd0\u884c\u524d\u540e\u7684\u65f6\u95f4\u6233\uff0c\u53ef\u4ee5\u8ba1\u7b97\u51fa\u4ee3\u7801\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()<\/p>\n<h2><strong>\u9700\u8981\u6d4b\u91cf\u8fd0\u884c\u65f6\u95f4\u7684\u4ee3\u7801\u6bb5<\/strong><\/h2>\n<p>for i in range(1000000):<\/p>\n<p>    pass<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;\u8fd0\u884c\u65f6\u95f4: {end_time - start_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<\/li>\n<li>\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7cbe\u5ea6\u8f83\u4f4e\uff0c\u53ea\u80fd\u7cbe\u786e\u5230\u79d2\u7ea7\u522b<\/li>\n<li>\u53d7\u5230\u7cfb\u7edf\u65f6\u95f4\u8bbe\u7f6e\u548c\u65f6\u949f\u7cbe\u5ea6\u7684\u5f71\u54cd<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528timeit\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>timeit<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u4e13\u95e8\u7528\u4e8e\u6027\u80fd\u6d4b\u8bd5\u7684\u6a21\u5757\uff0c\u5b83\u53ef\u4ee5\u66f4\u7cbe\u786e\u5730\u6d4b\u91cf\u4ee3\u7801\u7247\u6bb5\u7684\u6267\u884c\u65f6\u95f4\u3002<code>timeit<\/code>\u6a21\u5757\u901a\u8fc7\u591a\u6b21\u8fd0\u884c\u4ee3\u7801\u7247\u6bb5\u6765\u51cf\u5c11\u5076\u7136\u56e0\u7d20\u7684\u5f71\u54cd\uff0c\u4ece\u800c\u63d0\u4f9b\u66f4\u51c6\u786e\u7684\u6d4b\u91cf\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<h2><strong>\u9700\u8981\u6d4b\u91cf\u8fd0\u884c\u65f6\u95f4\u7684\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<p>execution_time = timeit.timeit(stmt=code_to_test, number=100)<\/p>\n<p>print(f&quot;\u8fd0\u884c\u65f6\u95f4: {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\u5408\u5fae\u79d2\u7ea7\u522b\u7684\u6d4b\u91cf<\/li>\n<li>\u80fd\u591f\u591a\u6b21\u8fd0\u884c\u4ee3\u7801\u7247\u6bb5\uff0c\u51cf\u5c11\u5076\u7136\u8bef\u5dee<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9002\u7528\u4e8e\u8f83\u5c0f\u7684\u4ee3\u7801\u7247\u6bb5<\/li>\n<li>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u4f1a\u589e\u52a0\u4ee3\u7801\u7684\u590d\u6742\u6027<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528datetime\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u53ef\u4ee5\u901a\u8fc7\u8bb0\u5f55\u4ee3\u7801\u8fd0\u884c\u524d\u540e\u7684\u65f6\u95f4\u70b9\u6765\u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4\u3002\u867d\u7136\u4e0d\u5982<code>timeit<\/code>\u6a21\u5757\u7cbe\u786e\uff0c\u4f46\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u66ff\u4ee3\u65b9\u6848\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>\u9700\u8981\u6d4b\u91cf\u8fd0\u884c\u65f6\u95f4\u7684\u4ee3\u7801\u6bb5<\/strong><\/h2>\n<p>for i in range(1000000):<\/p>\n<p>    pass<\/p>\n<p>end_time = datetime.now()<\/p>\n<p>print(f&quot;\u8fd0\u884c\u65f6\u95f4: {end_time - start_time}&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<\/li>\n<li>\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7cbe\u5ea6\u8f83\u4f4e\uff0c\u53ea\u80fd\u7cbe\u786e\u5230\u79d2\u7ea7\u522b<\/li>\n<li>\u53d7\u5230\u7cfb\u7edf\u65f6\u95f4\u8bbe\u7f6e\u548c\u65f6\u949f\u7cbe\u5ea6\u7684\u5f71\u54cd<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u5206\u6790\u4ee3\u7801\u7684\u74f6\u9888<\/h3>\n<\/p>\n<p><p>\u68c0\u6d4b\u4ee3\u7801\u8fd0\u884c\u65f6\u95f4\u7684\u6700\u7ec8\u76ee\u7684\u662f\u4e3a\u4e86\u4f18\u5316\u6027\u80fd\u3002\u9664\u4e86\u6d4b\u91cf\u6574\u4f53\u8fd0\u884c\u65f6\u95f4\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u5206\u6790\u4ee3\u7801\u7684\u74f6\u9888\uff0c\u627e\u51fa\u6027\u80fd\u4f4e\u4e0b\u7684\u90e8\u5206\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u5206\u6790\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528cProfile\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>cProfile<\/code>\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u63d0\u4f9b\u8be6\u7ec6\u7684\u51fd\u6570\u8c03\u7528\u7edf\u8ba1\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cProfile<\/p>\n<p>def test_function():<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        pass<\/p>\n<p>cProfile.run(&#39;test_function()&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u63d0\u4f9b\u8be6\u7ec6\u7684\u51fd\u6570\u8c03\u7528\u7edf\u8ba1\u4fe1\u606f<\/li>\n<li>\u9002\u7528\u4e8e\u590d\u6742\u7684\u6027\u80fd\u5206\u6790<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u8f93\u51fa\u4fe1\u606f\u8f83\u591a\uff0c\u9700\u4ed4\u7ec6\u5206\u6790<\/li>\n<li>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u4f1a\u589e\u52a0\u4ee3\u7801\u7684\u590d\u6742\u6027<\/li>\n<\/ul>\n<p><h4>\u4f7f\u7528line_profiler\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>line_profiler<\/code>\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u6a21\u5757\uff0c\u53ef\u4ee5\u63d0\u4f9b\u9010\u884c\u7684\u6027\u80fd\u5206\u6790\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from line_profiler import LineProfiler<\/p>\n<p>def test_function():<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        pass<\/p>\n<p>profile = LineProfiler()<\/p>\n<p>profile.add_function(test_function)<\/p>\n<p>profile.enable_by_count()<\/p>\n<p>test_function()<\/p>\n<p>profile.print_stats()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u63d0\u4f9b\u9010\u884c\u7684\u6027\u80fd\u5206\u6790\u4fe1\u606f<\/li>\n<li>\u9002\u7528\u4e8e\u590d\u6742\u7684\u6027\u80fd\u5206\u6790<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u5b89\u88c5\u7b2c\u4e09\u65b9\u6a21\u5757<\/li>\n<li>\u8f93\u51fa\u4fe1\u606f\u8f83\u591a\uff0c\u9700\u4ed4\u7ec6\u5206\u6790<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u4e0d\u540c\u65b9\u6cd5\u8fdb\u884c\u7efc\u5408\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5355\u4e00\u7684\u65b9\u6cd5\u53ef\u80fd\u65e0\u6cd5\u5168\u9762\u53cd\u6620\u4ee3\u7801\u7684\u6027\u80fd\u72b6\u51b5\u3002\u56e0\u6b64\uff0c\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u7efc\u5408\u5206\u6790\u662f\u4e00\u4e2a\u597d\u7684\u9009\u62e9\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u6216<code>datetime<\/code>\u6a21\u5757\u8fdb\u884c\u6574\u4f53\u6d4b\u91cf\uff0c\u518d\u4f7f\u7528<code>cProfile<\/code>\u6216<code>line_profiler<\/code>\u8fdb\u884c\u8be6\u7ec6\u5206\u6790\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7efc\u5408\u4f7f\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u5168\u9762\u5730\u4e86\u89e3\u4ee3\u7801\u7684\u6027\u80fd\u74f6\u9888\uff0c\u8fdb\u800c\u8fdb\u884c\u6709\u9488\u5bf9\u6027\u7684\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f18\u5316\u4ee3\u7801\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5728\u68c0\u6d4b\u4ee3\u7801\u8fd0\u884c\u65f6\u95f4\u540e\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u8fdb\u884c\u4f18\u5316\uff0c\u4ee5\u63d0\u5347\u4ee3\u7801\u7684\u6574\u4f53\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u4f18\u5316\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5c3d\u91cf\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u548c\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5faa\u73af\u4e2d\u7684\u5e38\u91cf\u8ba1\u7b97\u79fb\u5230\u5faa\u73af\u5916\u90e8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f18\u5316\u524d<\/p>\n<p>for i in range(1000000):<\/p>\n<p>    result = i * 2 * 3.14159<\/p>\n<h2><strong>\u4f18\u5316\u540e<\/strong><\/h2>\n<p>constant = 2 * 3.14159<\/p>\n<p>for i in range(1000000):<\/p>\n<p>    result = i * constant<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u51cf\u5c11\u8ba1\u7b97\u6b21\u6570\uff0c\u63d0\u5347\u6027\u80fd<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u4ed4\u7ec6\u5206\u6790\u4ee3\u7801\uff0c\u627e\u51fa\u53ef\u4ee5\u4f18\u5316\u7684\u90e8\u5206<\/li>\n<\/ul>\n<p><h4>\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u4ee3\u7801\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>set<\/code>\u800c\u4e0d\u662f<code>list<\/code>\u8fdb\u884c\u67e5\u627e\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5927\u5e45\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f18\u5316\u524d<\/p>\n<p>data = [1, 2, 3, 4, 5]<\/p>\n<p>if 3 in data:<\/p>\n<p>    pass<\/p>\n<h2><strong>\u4f18\u5316\u540e<\/strong><\/h2>\n<p>data = {1, 2, 3, 4, 5}<\/p>\n<p>if 3 in data:<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u63d0\u5347\u67e5\u627e\u64cd\u4f5c\u7684\u6548\u7387<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/li>\n<\/ul>\n<p><h4>\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u63d0\u5347\u4ee3\u7801\u7684\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\u5206\u914d\u5230\u591a\u4e2a\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u4e2d\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def worker():<\/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=worker)<\/p>\n<p>    threads.append(thread)<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u63d0\u5347\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\u7684\u6027\u80fd<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u589e\u52a0\u4ee3\u7801\u590d\u6742\u6027<\/li>\n<li>\u9700\u8981\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/li>\n<\/ul>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u68c0\u6d4bPython\u4ee3\u7801\u8fd0\u884c\u65f6\u95f4\u662f\u6027\u80fd\u4f18\u5316\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u901a\u8fc7\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u3001<code>timeit<\/code>\u6a21\u5757\u3001<code>datetime<\/code>\u6a21\u5757\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u5f97\u4ee3\u7801\u7684\u8fd0\u884c\u65f6\u95f4\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>cProfile<\/code>\u3001<code>line_profiler<\/code>\u7b49\u5de5\u5177\u8fdb\u884c\u8be6\u7ec6\u7684\u6027\u80fd\u5206\u6790\uff0c\u627e\u51fa\u4ee3\u7801\u7684\u74f6\u9888\u3002\u6700\u540e\uff0c\u901a\u8fc7\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u3001\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u3001\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u4ee3\u7801\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u6d4b\u91cf\u7279\u5b9a\u4ee3\u7801\u5757\u7684\u6267\u884c\u65f6\u95f4\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528 Python \u7684\u5185\u7f6e\u6a21\u5757 <code>time<\/code> \u6765\u6d4b\u91cf\u7279\u5b9a\u4ee3\u7801\u5757\u7684\u6267\u884c\u65f6\u95f4\u3002\u901a\u8fc7\u5728\u4ee3\u7801\u5757\u524d\u540e\u6dfb\u52a0\u65f6\u95f4\u6233\uff0c\u53ef\u4ee5\u8ba1\u7b97\u51fa\u8fd0\u884c\u65f6\u957f\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()\n\nprint(f&quot;\u8fd0\u884c\u65f6\u95f4: {end_time - start_time} \u79d2&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u4e8e\u5feb\u901f\u6d4b\u91cf\u7b80\u5355\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u5206\u6790\u4ee3\u7801\u6027\u80fd\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528 <code>time<\/code> \u6a21\u5757\u5916\uff0cPython \u8fd8\u6709\u5176\u4ed6\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u6bd4\u5982 <code>cProfile<\/code> \u548c <code>line_profiler<\/code>\u3002<code>cProfile<\/code> \u53ef\u4ee5\u63d0\u4f9b\u66f4\u8be6\u7ec6\u7684\u6027\u80fd\u5206\u6790\uff0c\u5305\u62ec\u51fd\u6570\u8c03\u7528\u6b21\u6570\u548c\u8fd0\u884c\u65f6\u95f4\uff0c\u9002\u5408\u4e8e\u590d\u6742\u9879\u76ee\u7684\u6027\u80fd\u4f18\u5316\u3002\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import cProfile\n\ndef my_function():\n    # \u4f60\u7684\u4ee3\u7801\u903b\u8f91\n\ncProfile.run(&#39;my_function()&#39;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u6bb5\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u5f97\u5230\u51fd\u6570\u7684\u8be6\u7ec6\u6267\u884c\u65f6\u95f4\u7edf\u8ba1\u3002<\/p>\n<p><strong>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u5982\u4f55\u6709\u6548\u5730\u76d1\u63a7\u4ee3\u7801\u6027\u80fd\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u6027\u80fd\u76d1\u63a7\u5de5\u5177\uff0c\u5982 <code>Py-Spy<\/code> \u6216 <code>Scalene<\/code>\u3002\u8fd9\u4e9b\u5de5\u5177\u80fd\u591f\u5728\u4e0d\u4fee\u6539\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u5b9e\u65f6\u76d1\u63a7\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u63d0\u4f9b CPU \u548c\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u7684\u8be6\u7ec6\u62a5\u544a\u3002\u8fd9\u5bf9\u4e8e\u5b9a\u4f4d\u6027\u80fd\u74f6\u9888\u548c\u4f18\u5316\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002\u5b89\u88c5\u548c\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u76f8\u5bf9\u7b80\u5355\uff0c\u5177\u4f53\u53ef\u53c2\u8003\u5b83\u4eec\u7684\u5b98\u65b9\u6587\u6863\u83b7\u53d6\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u68c0\u6d4bPython\u4ee3\u7801\u8fd0\u884c\u65f6\u95f4\uff1a\u4f7f\u7528time\u6a21\u5757\u3001\u4f7f\u7528timeit\u6a21\u5757\u3001\u4f7f\u7528datetime\u6a21\u5757\u3001\u5206\u6790\u4ee3\u7801\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1037016,"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\/1037009"}],"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=1037009"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1037009\/revisions"}],"predecessor-version":[{"id":1037023,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1037009\/revisions\/1037023"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1037016"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1037009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1037009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1037009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}