{"id":1159721,"date":"2025-01-13T18:56:16","date_gmt":"2025-01-13T10:56:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1159721.html"},"modified":"2025-01-13T18:56:19","modified_gmt":"2025-01-13T10:56:19","slug":"python%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e5%86%85%e5%ad%98%e6%ba%a2%e5%87%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1159721.html","title":{"rendered":"python\u5982\u4f55\u5904\u7406\u5185\u5b58\u6ea2\u51fa"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25201408\/fe23f201-1252-4a65-aef8-1c9da156c5d2.webp\" alt=\"python\u5982\u4f55\u5904\u7406\u5185\u5b58\u6ea2\u51fa\" \/><\/p>\n<p><p> Python\u5904\u7406\u5185\u5b58\u6ea2\u51fa\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f18\u5316\u4ee3\u7801\u3001\u4f7f\u7528\u751f\u6210\u5668\u3001\u589e\u52a0\u865a\u62df\u5185\u5b58\u3001\u5206\u6279\u5904\u7406\u6570\u636e\u3001\u4f7f\u7528\u5185\u5b58\u7ba1\u7406\u5de5\u5177\u3002<strong>\u4f18\u5316\u4ee3\u7801<\/strong>\u662f\u6700\u6709\u6548\u7684\u65b9\u5f0f\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u4ece\u6839\u672c\u4e0a\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u901a\u8fc7\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u5bf9\u8c61\u521b\u5efa\u3001\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u663e\u8457\u964d\u4f4e\u5185\u5b58\u6d88\u8017\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f18\u5316\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4f18\u5316\u4ee3\u7801\u662f\u5904\u7406\u5185\u5b58\u6ea2\u51fa\u7684\u9996\u8981\u65b9\u6cd5\u3002Python\u4e2d\u6709\u8bb8\u591a\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u9002\u5f53\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>deque<\/code>\u4ee3\u66ff<code>list<\/code>\uff0c\u5728\u9700\u8981\u9891\u7e41\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u65f6\u53ef\u4ee5\u66f4\u9ad8\u6548\u3002\u4f7f\u7528<code>set<\/code>\u4ee3\u66ff<code>list<\/code>\u8fdb\u884c\u6210\u5458\u68c0\u6d4b\u64cd\u4f5c\uff0c\u56e0\u4e3a<code>set<\/code>\u7684\u67e5\u627e\u590d\u6742\u5ea6\u662fO(1)\uff0c\u800c<code>list<\/code>\u662fO(n)\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<h2><strong>Using deque for efficient pop and append operations<\/strong><\/h2>\n<p>queue = deque([1, 2, 3, 4])<\/p>\n<p>queue.append(5)<\/p>\n<p>queue.popleft()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u907f\u514d\u91cd\u590d\u521b\u5efa\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u5728\u5faa\u73af\u4e2d\u907f\u514d\u91cd\u590d\u521b\u5efa\u5bf9\u8c61\uff0c\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5c3d\u91cf\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u800c\u4e0d\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u6216\u8005\u5728\u5faa\u73af\u5916\u521b\u5efa\u5bf9\u8c61\u5e76\u5728\u5faa\u73af\u4e2d\u590d\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Avoid creating new objects in a loop<\/p>\n<p>large_list = [i for i in range(100000)]<\/p>\n<p>sum = 0<\/p>\n<p>for number in large_list:<\/p>\n<p>    sum += number<\/p>\n<p>print(sum)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u5904\u7406\u5927\u6570\u636e\u96c6\u7684\u6709\u6548\u5de5\u5177\u3002\u4e0e\u5217\u8868\u4e0d\u540c\uff0c\u751f\u6210\u5668\u4e0d\u4f1a\u4e00\u6b21\u6027\u5c06\u6240\u6709\u6570\u636e\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\uff0c\u800c\u662f\u9010\u4e2a\u751f\u6210\u6570\u636e\u9879\uff0c\u8fd9\u6837\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Using a generator to handle large data<\/p>\n<p>def large_data_generator():<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        yield i<\/p>\n<p>for data in large_data_generator():<\/p>\n<p>    process(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u589e\u52a0\u865a\u62df\u5185\u5b58<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u589e\u52a0\u865a\u62df\u5185\u5b58\u6765\u6682\u65f6\u89e3\u51b3\u5185\u5b58\u6ea2\u51fa\u95ee\u9898\u3002\u865a\u62df\u5185\u5b58\u5141\u8bb8\u64cd\u4f5c\u7cfb\u7edf\u4f7f\u7528\u786c\u76d8\u7a7a\u95f4\u6765\u6269\u5c55\u7269\u7406\u5185\u5b58\uff0c\u4ece\u800c\u5728\u5185\u5b58\u4e0d\u8db3\u65f6\u63d0\u4f9b\u989d\u5916\u7684\u7f13\u51b2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># Linux example to add swap space<\/p>\n<p>sudo fallocate -l 4G \/swapfile<\/p>\n<p>sudo chmod 600 \/swapfile<\/p>\n<p>sudo mkswap \/swapfile<\/p>\n<p>sudo swapon \/swapfile<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5206\u6279\u5904\u7406\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5206\u6210\u5c0f\u6279\u6b21\u5904\u7406\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u5bfc\u81f4\u7684\u5185\u5b58\u6ea2\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Batch processing data<\/p>\n<p>def process_in_batches(data, batch_size):<\/p>\n<p>    for i in range(0, len(data), batch_size):<\/p>\n<p>        batch = data[i:i + batch_size]<\/p>\n<p>        process(batch)<\/p>\n<p>data = [i for i in range(1000000)]<\/p>\n<p>process_in_batches(data, 10000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5185\u5b58\u7ba1\u7406\u5de5\u5177<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u5b58\u7ba1\u7406\u5de5\u5177\u548c\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u76d1\u63a7\u548c\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>gc<\/code>\u6a21\u5757\u624b\u52a8\u89e6\u53d1\u5783\u573e\u56de\u6536\uff0c\u6216\u8005\u4f7f\u7528<code>memory_profiler<\/code>\u76d1\u63a7\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gc<\/p>\n<p>import memory_profiler<\/p>\n<h2><strong>Manual garbage collection<\/strong><\/h2>\n<p>gc.collect()<\/p>\n<h2><strong>Memory profiling<\/strong><\/h2>\n<p>@memory_profiler.profile<\/p>\n<p>def my_function():<\/p>\n<p>    # Function implementation<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4ee3\u7801\u793a\u4f8b\u8be6\u89e3<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u4e0a\u8ff0\u65b9\u6cd5\u7684\u5b9e\u9645\u5e94\u7528\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\u8fdb\u884c\u8be6\u7ec6\u8bb2\u89e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from collections import deque<\/p>\n<h2><strong>Example 1: Optimizing data structure usage<\/strong><\/h2>\n<p>def optimize_data_structure():<\/p>\n<p>    data_list = [i for i in range(1000000)]<\/p>\n<p>    data_set = set(data_list)<\/p>\n<p>    return data_set<\/p>\n<h2><strong>Example 2: Using a generator to handle large data<\/strong><\/h2>\n<p>def large_data_generator():<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        yield i<\/p>\n<h2><strong>Example 3: Manual garbage collection<\/strong><\/h2>\n<p>def manual_garbage_collection():<\/p>\n<p>    large_list = [i for i in range(1000000)]<\/p>\n<p>    del large_list<\/p>\n<p>    gc.collect()<\/p>\n<h2><strong>Example 4: Batch processing data<\/strong><\/h2>\n<p>def batch_processing(data, batch_size):<\/p>\n<p>    for i in range(0, len(data), batch_size):<\/p>\n<p>        batch = data[i:i + batch_size]<\/p>\n<p>        process(batch)<\/p>\n<h2><strong>Example 5: Memory profiling<\/strong><\/h2>\n<p>@memory_profiler.profile<\/p>\n<p>def memory_intensive_function():<\/p>\n<p>    large_list = [i for i in range(1000000)]<\/p>\n<p>    return large_list<\/p>\n<p>def process(batch):<\/p>\n<p>    # Dummy processing function<\/p>\n<p>    pass<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    # Example usage of optimized data structure<\/p>\n<p>    optimized_data = optimize_data_structure()<\/p>\n<p>    print(&quot;Optimized data structure used.&quot;)<\/p>\n<p>    # Example usage of generator<\/p>\n<p>    for data in large_data_generator():<\/p>\n<p>        process(data)<\/p>\n<p>    print(&quot;Generator used for large data.&quot;)<\/p>\n<p>    # Example usage of manual garbage collection<\/p>\n<p>    manual_garbage_collection()<\/p>\n<p>    print(&quot;Manual garbage collection triggered.&quot;)<\/p>\n<p>    # Example usage of batch processing<\/p>\n<p>    data = [i for i in range(1000000)]<\/p>\n<p>    batch_processing(data, 10000)<\/p>\n<p>    print(&quot;Batch processing completed.&quot;)<\/p>\n<p>    # Example usage of memory profiling<\/p>\n<p>    memory_intensive_function()<\/p>\n<p>    print(&quot;Memory profiling completed.&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406Python\u4e2d\u7684\u5185\u5b58\u6ea2\u51fa\u95ee\u9898\u3002<strong>\u4f18\u5316\u4ee3\u7801\u3001\u4f7f\u7528\u751f\u6210\u5668\u3001\u589e\u52a0\u865a\u62df\u5185\u5b58\u3001\u5206\u6279\u5904\u7406\u6570\u636e\u3001\u4f7f\u7528\u5185\u5b58\u7ba1\u7406\u5de5\u5177<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u5e2e\u52a9\u51cf\u5c11\u5185\u5b58\u6d88\u8017\uff0c\u8fd8\u53ef\u4ee5\u63d0\u5347\u7a0b\u5e8f\u7684\u6574\u4f53\u6027\u80fd\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u7b56\u7565\uff0c\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4ece\u800c\u786e\u4fdd\u7a0b\u5e8f\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\u4ecd\u80fd\u9ad8\u6548\u7a33\u5b9a\u8fd0\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5185\u5b58\u6ea2\u51fa\u5728Python\u4e2d\u5e38\u89c1\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5185\u5b58\u6ea2\u51fa\u901a\u5e38\u662f\u7531\u4e8e\u7a0b\u5e8f\u8bd5\u56fe\u4f7f\u7528\u8d85\u8fc7\u7cfb\u7edf\u53ef\u7528\u5185\u5b58\u7684\u8d44\u6e90\uff0c\u8fd9\u53ef\u80fd\u662f\u7531\u4e8e\u6570\u636e\u7ed3\u6784\u8fc7\u5927\u3001\u65e0\u9650\u9012\u5f52\u3001\u6216\u6301\u7eed\u521b\u5efa\u5bf9\u8c61\u800c\u4e0d\u91ca\u653e\u5185\u5b58\u7b49\u539f\u56e0\u3002\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u6216\u590d\u6742\u7b97\u6cd5\u65f6\uff0c\u5f00\u53d1\u8005\u9700\u8981\u6ce8\u610f\u5185\u5b58\u7684\u4f7f\u7528\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u6d4b\u6211\u7684Python\u7a0b\u5e8f\u662f\u5426\u5b58\u5728\u5185\u5b58\u6cc4\u6f0f\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5de5\u5177\u6765\u68c0\u6d4b\u5185\u5b58\u6cc4\u6f0f\uff0c\u5982<code>objgraph<\/code>\u3001<code>memory_profiler<\/code>\u548c<code>guppy<\/code>\u7b49\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8ddf\u8e2a\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c\u8bc6\u522b\u672a\u88ab\u91ca\u653e\u7684\u5bf9\u8c61\uff0c\u4ece\u800c\u5e2e\u52a9\u627e\u5230\u53ef\u80fd\u7684\u5185\u5b58\u6cc4\u6f0f\u70b9\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u65f6\u6709\u54ea\u4e9b\u6280\u5de7\u53ef\u4ee5\u4f18\u5316\u5185\u5b58\u4f7f\u7528\uff1f<\/strong><br \/>\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u4f7f\u7528\u751f\u6210\u5668\u4ee3\u66ff\u5217\u8868\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u5185\u5b58\u5360\u7528\uff0c\u907f\u514d\u5728\u5185\u5b58\u4e2d\u540c\u65f6\u5b58\u50a8\u6240\u6709\u6570\u636e\u3002\u4f7f\u7528<code>__slots__<\/code>\u6765\u9650\u5236\u5bf9\u8c61\u7684\u5c5e\u6027\u6570\u91cf\u4e5f\u80fd\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002\u6b64\u5916\uff0c\u5b9a\u671f\u4f7f\u7528<code>gc.collect()<\/code>\u624b\u52a8\u89e6\u53d1\u5783\u573e\u56de\u6536\u53ef\u4ee5\u5e2e\u52a9\u91ca\u653e\u4e0d\u518d\u4f7f\u7528\u7684\u5185\u5b58\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5904\u7406\u5185\u5b58\u6ea2\u51fa\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f18\u5316\u4ee3\u7801\u3001\u4f7f\u7528\u751f\u6210\u5668\u3001\u589e\u52a0\u865a\u62df\u5185\u5b58\u3001\u5206\u6279\u5904\u7406\u6570\u636e\u3001\u4f7f\u7528\u5185\u5b58\u7ba1\u7406\u5de5\u5177\u3002\u4f18\u5316 [&hellip;]","protected":false},"author":3,"featured_media":1159734,"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\/1159721"}],"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=1159721"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159721\/revisions"}],"predecessor-version":[{"id":1159735,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159721\/revisions\/1159735"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1159734"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1159721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1159721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1159721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}