{"id":1172465,"date":"2025-01-15T16:49:45","date_gmt":"2025-01-15T08:49:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1172465.html"},"modified":"2025-01-15T16:49:48","modified_gmt":"2025-01-15T08:49:48","slug":"%e5%a6%82%e4%bd%95%e8%ae%a9python%e8%bf%90%e8%a1%8c%e7%9a%84%e6%9b%b4%e5%bf%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1172465.html","title":{"rendered":"\u5982\u4f55\u8ba9python\u8fd0\u884c\u7684\u66f4\u5feb"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074519\/38e39043-52c1-42a1-bd84-b48e1bc57dfc.webp\" alt=\"\u5982\u4f55\u8ba9python\u8fd0\u884c\u7684\u66f4\u5feb\" \/><\/p>\n<p><p> <strong>\u8981\u8ba9Python\u8fd0\u884c\u66f4\u5feb\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f18\u5316\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u3001\u5229\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u3001\u7f16\u8bd1\u4e3aC\u6269\u5c55\u3001\u4f7f\u7528JIT\u7f16\u8bd1\u5668\u5982PyPy\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u4f18\u5316\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u662f\u63d0\u9ad8Python\u4ee3\u7801\u6027\u80fd\u7684\u5173\u952e\u3002\u9009\u62e9\u9002\u5f53\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u54c8\u5e0c\u8868\u6765\u66ff\u4ee3\u5217\u8868\u8fdb\u884c\u67e5\u627e\u64cd\u4f5c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6548\u7387\u3002\u4f18\u5316\u7b97\u6cd5\u65f6\uff0c\u9700\u8981\u8003\u8651\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5e76\u5c3d\u91cf\u51cf\u5c11\u5d4c\u5957\u5faa\u73af\u548c\u9012\u5f52\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f18\u5316\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u4f18\u5316\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u662f\u63d0\u9ad8Python\u4ee3\u7801\u6027\u80fd\u7684\u6838\u5fc3\u65b9\u6cd5\u4e4b\u4e00\u3002\u5408\u9002\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\u548c\u5185\u5b58\u6d88\u8017\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5177\u4f53\u7684\u4f18\u5316\u7b56\u7565\uff1a<\/p>\n<\/p>\n<p><h4>1.1\u3001\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e0b\u6027\u80fd\u4f1a\u6709\u5f88\u5927\u5dee\u5f02\u3002\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\u5305\u62ec\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\u548c\u5143\u7ec4\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5217\u8868<\/strong>\uff1a\u9002\u7528\u4e8e\u987a\u5e8f\u8bbf\u95ee\u548c\u9700\u8981\u52a8\u6001\u8c03\u6574\u5927\u5c0f\u7684\u60c5\u51b5\u3002\u4f46\u5728\u8fdb\u884c\u67e5\u627e\u548c\u63d2\u5165\u64cd\u4f5c\u65f6\uff0c\u6548\u7387\u8f83\u4f4e\u3002<\/li>\n<li><strong>\u5b57\u5178<\/strong>\uff1a\u57fa\u4e8e\u54c8\u5e0c\u8868\u5b9e\u73b0\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u60c5\u51b5\u3002\u5bf9\u4e8e\u952e\u503c\u5bf9\u5b58\u50a8\u6570\u636e\u975e\u5e38\u9ad8\u6548\u3002<\/li>\n<li><strong>\u96c6\u5408<\/strong>\uff1a\u4e5f\u57fa\u4e8e\u54c8\u5e0c\u8868\u5b9e\u73b0\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u552f\u4e00\u5143\u7d20\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u5143\u7ec4<\/strong>\uff1a\u4e0d\u53ef\u53d8\u7684\u5e8f\u5217\uff0c\u9002\u7528\u4e8e\u9700\u8981\u56fa\u5b9a\u5927\u5c0f\u548c\u4e0d\u53ef\u53d8\u6570\u636e\u7684\u60c5\u51b5\u3002\u7531\u4e8e\u5176\u4e0d\u53ef\u53d8\u6027\uff0c\u5143\u7ec4\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6bd4\u5217\u8868\u66f4\u9ad8\u6548\u3002<\/li>\n<\/ul>\n<p><h4>1.2\u3001\u4f18\u5316\u7b97\u6cd5<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u7b97\u6cd5\u4f18\u5316\u7b56\u7565\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u907f\u514d\u5d4c\u5957\u5faa\u73af<\/strong>\uff1a\u5d4c\u5957\u5faa\u73af\u4f1a\u5bfc\u81f4\u65f6\u95f4\u590d\u6742\u5ea6\u5448\u6307\u6570\u589e\u957f\uff0c\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u3002\u53ef\u4ee5\u901a\u8fc7\u9884\u5904\u7406\u6570\u636e\u6216\u4f7f\u7528\u9002\u5f53\u7684\u6570\u636e\u7ed3\u6784\u6765\u51cf\u5c11\u5d4c\u5957\u5faa\u73af\u7684\u4f7f\u7528\u3002<\/li>\n<li><strong>\u4f7f\u7528\u7f13\u5b58<\/strong>\uff1a\u5bf9\u4e8e\u9700\u8981\u591a\u6b21\u8ba1\u7b97\u7684\u7ed3\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\uff08\u4f8b\u5982\u5b57\u5178\uff09\u6765\u5b58\u50a8\u4e2d\u95f4\u7ed3\u679c\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002<\/li>\n<li><strong>\u5206\u6cbb\u6cd5<\/strong>\uff1a\u5c06\u5927\u95ee\u9898\u5206\u89e3\u4e3a\u5c0f\u95ee\u9898\u5206\u522b\u89e3\u51b3\uff0c\u7136\u540e\u5408\u5e76\u7ed3\u679c\u3002\u5206\u6cbb\u6cd5\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002<\/li>\n<li><strong>\u52a8\u6001\u89c4\u5212<\/strong>\uff1a\u9002\u7528\u4e8e\u5b58\u5728\u91cd\u53e0\u5b50\u95ee\u9898\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u95ee\u9898\u3002\u901a\u8fc7\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\u6765\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5e93<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5185\u7f6e\u51fd\u6570\u548c\u5e93\uff0c\u5229\u7528\u8fd9\u4e9b\u51fd\u6570\u548c\u5e93\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u8fd0\u884c\u6548\u7387\u3002\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u901a\u5e38\u662f\u7528C\u8bed\u8a00\u5b9e\u73b0\u7684\uff0c\u6027\u80fd\u6bd4\u7eafPython\u4ee3\u7801\u9ad8\u6548\u5f97\u591a\u3002<\/p>\n<\/p>\n<p><h4>2.1\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/h4>\n<\/p>\n<p><p>Python\u5185\u7f6e\u51fd\u6570\u7ecf\u8fc7\u9ad8\u5ea6\u4f18\u5316\uff0c\u6027\u80fd\u901a\u5e38\u4f18\u4e8e\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>sum()<\/strong>\uff1a\u7528\u4e8e\u8ba1\u7b97\u5e8f\u5217\u7684\u603b\u548c\uff0c\u6bd4\u4f7f\u7528\u5faa\u73af\u7d2f\u52a0\u66f4\u9ad8\u6548\u3002<\/li>\n<li><strong>max()<\/strong>\u548c<strong>min()<\/strong>\uff1a\u7528\u4e8e\u627e\u51fa\u5e8f\u5217\u4e2d\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\uff0c\u6bd4\u4f7f\u7528\u5faa\u73af\u67e5\u627e\u66f4\u9ad8\u6548\u3002<\/li>\n<li><strong>sorted()<\/strong>\uff1a\u7528\u4e8e\u5bf9\u5e8f\u5217\u8fdb\u884c\u6392\u5e8f\uff0c\u6bd4\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u7b97\u6cd5\u66f4\u9ad8\u6548\u3002<\/li>\n<\/ul>\n<p><h4>2.2\u3001\u4f7f\u7528\u6807\u51c6\u5e93<\/h4>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u4e2d\u5305\u542b\u4e86\u5927\u91cf\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>collections.Counter<\/strong>\uff1a\u7528\u4e8e\u8ba1\u6570\uff0c\u6027\u80fd\u4f18\u4e8e\u624b\u52a8\u5b9e\u73b0\u7684\u8ba1\u6570\u5668\u3002<\/li>\n<li><strong>itertools<\/strong>\uff1a\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u8fed\u4ee3\u5668\u51fd\u6570\uff0c\u7528\u4e8e\u5904\u7406\u7ec4\u5408\u3001\u6392\u5217\u3001\u7b1b\u5361\u5c14\u79ef\u7b49\u64cd\u4f5c\u3002<\/li>\n<li><strong>heapq<\/strong>\uff1a\u63d0\u4f9b\u4e86\u5806\u961f\u5217\u7b97\u6cd5\uff0c\u7528\u4e8e\u5b9e\u73b0\u4f18\u5148\u7ea7\u961f\u5217\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u8fd0\u884c\u6548\u7387\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u7b56\u7565\uff1a<\/p>\n<\/p>\n<p><h4>3.1\u3001\u51cf\u5c11\u5168\u5c40\u53d8\u91cf\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5168\u5c40\u53d8\u91cf\u7684\u8bbf\u95ee\u901f\u5ea6\u6bd4\u5c40\u90e8\u53d8\u91cf\u6162\uff0c\u56e0\u4e3aPython\u9700\u8981\u67e5\u627e\u5168\u5c40\u4f5c\u7528\u57df\u3002\u5c3d\u91cf\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u6765\u63d0\u9ad8\u8bbf\u95ee\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>3.2\u3001\u907f\u514d\u91cd\u590d\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u591a\u6b21\u8ba1\u7b97\u7684\u7ed3\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\uff08\u4f8b\u5982\u5b57\u5178\uff09\u6765\u5b58\u50a8\u4e2d\u95f4\u7ed3\u679c\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u8ba1\u7b97\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u6765\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h4>3.3\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u5185\u5b58\u4f7f\u7528\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u751f\u6210\u6570\u636e\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u5c06\u6240\u6709\u6570\u636e\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002\u751f\u6210\u5668\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u5185\u5b58\u6d88\u8017\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5229\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u5229\u7528\u591a\u6838CPU\u7684\u5e76\u884c\u8ba1\u7b97\u80fd\u529b\u6765\u63d0\u9ad8\u4ee3\u7801\u7684\u8fd0\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>4.1\u3001\u591a\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u9002\u7528\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u4f8b\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u5199\u7b49\u3002Python\u7684GIL\uff08\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff09\u9650\u5236\u4e86CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u7684\u5e76\u884c\u6267\u884c\uff0c\u4f46\u5bf9\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u591a\u7ebf\u7a0b\u4ecd\u7136\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def task():<\/p>\n<p>    # \u6267\u884c\u4efb\u52a1<\/p>\n<p>    pass<\/p>\n<p>threads = []<\/p>\n<p>for i in range(10):<\/p>\n<p>    t = threading.Thread(target=task)<\/p>\n<p>    threads.append(t)<\/p>\n<p>    t.start()<\/p>\n<p>for t in threads:<\/p>\n<p>    t.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2\u3001\u591a\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><p>\u591a\u8fdb\u7a0b\u9002\u7528\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u53ef\u4ee5\u5229\u7528\u591a\u6838CPU\u7684\u8ba1\u7b97\u80fd\u529b\u6765\u63d0\u9ad8\u6027\u80fd\u3002Python\u7684<code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u8fdb\u7a0b\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def task():<\/p>\n<p>    # \u6267\u884c\u4efb\u52a1<\/p>\n<p>    pass<\/p>\n<p>processes = []<\/p>\n<p>for i in range(10):<\/p>\n<p>    p = multiprocessing.Process(target=task)<\/p>\n<p>    processes.append(p)<\/p>\n<p>    p.start()<\/p>\n<p>for p in processes:<\/p>\n<p>    p.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7f16\u8bd1\u4e3aC\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u4e3aC\u6269\u5c55\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002C\u8bed\u8a00\u7684\u6267\u884c\u901f\u5ea6\u6bd4Python\u5feb\u5f97\u591a\uff0c\u5c06\u5173\u952e\u90e8\u5206\u7528C\u8bed\u8a00\u5b9e\u73b0\u53ef\u4ee5\u63d0\u9ad8\u6574\u4f53\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>5.1\u3001\u4f7f\u7528Cython<\/h4>\n<\/p>\n<p><p>Cython\u662fPython\u7684\u4e00\u4e2a\u6269\u5c55\uff0c\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u4e3aC\u4ee3\u7801\uff0c\u7136\u540e\u7f16\u8bd1\u4e3aC\u6269\u5c55\u3002\u4f7f\u7528Cython\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8Python\u4ee3\u7801\u7684\u8fd0\u884c\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># example.pyx<\/p>\n<p>def add(int a, int b):<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-bash\">cythonize -i example.pyx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2\u3001\u4f7f\u7528ctypes<\/h4>\n<\/p>\n<p><p><code>ctypes<\/code>\u662fPython\u7684\u4e00\u4e2a\u6807\u51c6\u5e93\uff0c\u53ef\u4ee5\u8c03\u7528C\u8bed\u8a00\u7f16\u5199\u7684\u52a8\u6001\u94fe\u63a5\u5e93\u3002\u901a\u8fc7<code>ctypes<\/code>\u53ef\u4ee5\u5c06\u5173\u952e\u90e8\u5206\u7528C\u8bed\u8a00\u5b9e\u73b0\uff0c\u5e76\u5728Python\u4e2d\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/\/ example.c<\/p>\n<p>int add(int a, int b) {<\/p>\n<p>    return a + b;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<p>lib = ctypes.CDLL(&#39;.\/example.so&#39;)<\/p>\n<p>result = lib.add(1, 2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528JIT\u7f16\u8bd1\u5668<\/h3>\n<\/p>\n<p><p>JIT\uff08Just-In-Time\uff09\u7f16\u8bd1\u5668\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u5c06Python\u5b57\u8282\u7801\u7f16\u8bd1\u4e3a\u673a\u5668\u7801\uff0c\u4ece\u800c\u663e\u8457\u63d0\u9ad8\u8fd0\u884c\u901f\u5ea6\u3002PyPy\u662f\u4e00\u4e2a\u5e38\u7528\u7684JIT\u7f16\u8bd1\u5668\uff0c\u517c\u5bb9CPython\uff0c\u5e76\u4e14\u6027\u80fd\u66f4\u9ad8\u3002<\/p>\n<\/p>\n<p><h4>6.1\u3001\u5b89\u88c5\u548c\u4f7f\u7528PyPy<\/h4>\n<\/p>\n<p><p>PyPy\u662fPython\u7684\u4e00\u79cd\u9ad8\u6027\u80fd\u89e3\u91ca\u5668\uff0c\u5177\u6709JIT\u7f16\u8bd1\u529f\u80fd\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8Python\u4ee3\u7801\u7684\u8fd0\u884c\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u5b89\u88c5PyPy<\/p>\n<p>sudo apt-get install pypy<\/p>\n<h2><strong>\u4f7f\u7528PyPy\u8fd0\u884cPython\u811a\u672c<\/strong><\/h2>\n<p>pypy script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.2\u3001PyPy\u7684\u4f18\u70b9\u548c\u5c40\u9650\u6027<\/h4>\n<\/p>\n<p><p>PyPy\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8Python\u4ee3\u7801\u7684\u8fd0\u884c\u901f\u5ea6\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u5c40\u9650\u6027\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u517c\u5bb9\u6027<\/strong>\uff1a\u867d\u7136PyPy\u517c\u5bb9\u5927\u90e8\u5206CPython\u4ee3\u7801\uff0c\u4f46\u67d0\u4e9bC\u6269\u5c55\u5e93\u53ef\u80fd\u4e0d\u517c\u5bb9\u3002<\/li>\n<li><strong>\u542f\u52a8\u65f6\u95f4<\/strong>\uff1a\u7531\u4e8eJIT\u7f16\u8bd1\u7684\u5f00\u9500\uff0cPyPy\u7684\u542f\u52a8\u65f6\u95f4\u6bd4CPython\u7565\u957f\u3002<\/li>\n<li><strong>\u5185\u5b58\u5360\u7528<\/strong>\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0cPyPy\u7684\u5185\u5b58\u5360\u7528\u6bd4CPython\u9ad8\u3002<\/li>\n<\/ul>\n<p><h3>\u4e03\u3001\u4f18\u5316\u5185\u5b58\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5185\u5b58\u4f7f\u7528\u7684\u4f18\u5316\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5185\u5b58\u4f18\u5316\u7b56\u7565\uff1a<\/p>\n<\/p>\n<p><h4>7.1\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u751f\u6210\u6570\u636e\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u5c06\u6240\u6709\u6570\u636e\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002\u751f\u6210\u5668\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u5185\u5b58\u6d88\u8017\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def data_generator():<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        yield i<\/p>\n<p>for data in data_generator():<\/p>\n<p>    # \u5904\u7406\u6570\u636e<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7.2\u3001\u91ca\u653e\u4e0d\u518d\u4f7f\u7528\u7684\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u53ca\u65f6\u91ca\u653e\u4e0d\u518d\u4f7f\u7528\u7684\u5bf9\u8c61\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002Python\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u4f1a\u81ea\u52a8\u56de\u6536\u4e0d\u518d\u4f7f\u7528\u7684\u5bf9\u8c61\uff0c\u4f46\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u624b\u52a8\u91ca\u653e\u5bf9\u8c61\u53ef\u4ee5\u66f4\u5feb\u5730\u91ca\u653e\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [i for i in range(1000000)]<\/p>\n<h2><strong>\u5904\u7406\u6570\u636e<\/strong><\/h2>\n<p>del data<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u9ad8\u6548\u7684I\/O\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9ad8\u6548\u7684I\/O\u64cd\u4f5c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u6587\u4ef6\u548c\u7f51\u7edc\u8bf7\u6c42\u65f6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684I\/O\u4f18\u5316\u7b56\u7565\uff1a<\/p>\n<\/p>\n<p><h4>8.1\u3001\u4f7f\u7528\u7f13\u51b2I\/O<\/h4>\n<\/p>\n<p><p>\u7f13\u51b2I\/O\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6587\u4ef6\u8bfb\u5199\u6027\u80fd\u3002Python\u7684<code>open()<\/code>\u51fd\u6570\u652f\u6301\u7f13\u51b2I\/O\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7f13\u51b2\u533a\u5927\u5c0f\u6765\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;large_file.txt&#39;, &#39;r&#39;, buffering=8192) as f:<\/p>\n<p>    for line in f:<\/p>\n<p>        # \u5904\u7406\u6bcf\u4e00\u884c<\/p>\n<p>        pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>8.2\u3001\u4f7f\u7528\u5f02\u6b65I\/O<\/h4>\n<\/p>\n<p><p>\u5f02\u6b65I\/O\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7f51\u7edc\u8bf7\u6c42\u548c\u6587\u4ef6\u8bfb\u5199\u7684\u6027\u80fd\u3002Python\u7684<code>asyncio<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f02\u6b65I\/O\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def fetch_data():<\/p>\n<p>    # \u5f02\u6b65\u7f51\u7edc\u8bf7\u6c42<\/p>\n<p>    pass<\/p>\n<p>async def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    tasks = [fetch_data() for _ in range(10)]<\/p>\n<p>    await asyncio.gather(*tasks)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u6027\u80fd\u5206\u6790\u548c\u8c03\u8bd5<\/h3>\n<\/p>\n<p><p>\u6027\u80fd\u5206\u6790\u548c\u8c03\u8bd5\u53ef\u4ee5\u5e2e\u52a9\u53d1\u73b0\u6027\u80fd\u74f6\u9888\uff0c\u5e76\u8fdb\u884c\u9488\u5bf9\u6027\u7684\u4f18\u5316\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6027\u80fd\u5206\u6790\u5de5\u5177\uff1a<\/p>\n<\/p>\n<p><h4>9.1\u3001cProfile<\/h4>\n<\/p>\n<p><p><code>cProfile<\/code>\u662fPython\u7684\u4e00\u4e2a\u5185\u7f6e\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u5206\u6790\u4ee3\u7801\u7684\u8fd0\u884c\u65f6\u95f4\u548c\u8c03\u7528\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cProfile<\/p>\n<p>def main():<\/p>\n<p>    # \u4e3b\u51fd\u6570<\/p>\n<p>    pass<\/p>\n<p>cProfile.run(&#39;main()&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>9.2\u3001line_profiler<\/h4>\n<\/p>\n<p><p><code>line_profiler<\/code>\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u5bf9\u4ee3\u7801\u7684\u6bcf\u4e00\u884c\u8fdb\u884c\u6027\u80fd\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from line_profiler import LineProfiler<\/p>\n<p>def main():<\/p>\n<p>    # \u4e3b\u51fd\u6570<\/p>\n<p>    pass<\/p>\n<p>profiler = LineProfiler()<\/p>\n<p>profiler.add_function(main)<\/p>\n<p>profiler.run(&#39;main()&#39;)<\/p>\n<p>profiler.print_stats()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f18\u5316\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u3001\u5229\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u3001\u7f16\u8bd1\u4e3aC\u6269\u5c55\u3001\u4f7f\u7528JIT\u7f16\u8bd1\u5668\u5982PyPy\u3001\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u3001\u4f7f\u7528\u9ad8\u6548\u7684I\/O\u64cd\u4f5c\u4ee5\u53ca\u8fdb\u884c\u6027\u80fd\u5206\u6790\u548c\u8c03\u8bd5\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8Python\u4ee3\u7801\u7684\u8fd0\u884c\u901f\u5ea6\u3002\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u4f18\u5316\u7b56\u7565\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u7684\u6027\u80fd\u63d0\u5347\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u63d0\u9ad8Python\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\uff1f<\/strong><br \/>\u8981\u63d0\u9ad8Python\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\uff0c\u53ef\u4ee5\u4ece\u591a\u4e2a\u65b9\u9762\u5165\u624b\u3002\u9996\u5148\uff0c\u4f18\u5316\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u975e\u5e38\u91cd\u8981\u3002\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u8fd0\u884c\u65f6\u95f4\u3002\u5176\u6b21\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5e93\uff0c\u56e0\u4e3a\u5b83\u4eec\u901a\u5e38\u7ecf\u8fc7\u4f18\u5316\uff0c\u6267\u884c\u901f\u5ea6\u66f4\u5feb\u3002\u6b64\u5916\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5faa\u73af\u548c\u91cd\u590d\u8ba1\u7b97\u4e5f\u80fd\u63d0\u5347\u6027\u80fd\u3002\u8003\u8651\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u5145\u5206\u5229\u7528\u8ba1\u7b97\u8d44\u6e90\uff0c\u4ece\u800c\u52a0\u901f\u7a0b\u5e8f\u7684\u8fd0\u884c\u3002<\/p>\n<p><strong>\u54ea\u4e9b\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5e2e\u52a9\u63d0\u5347Python\u6027\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u6709\u6548\u63d0\u5347\u6027\u80fd\u3002\u4f8b\u5982\uff0cNumPy\u548cPandas\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u8868\u73b0\u4f18\u5f02\uff0c\u5c24\u5176\u662f\u5728\u6570\u503c\u8ba1\u7b97\u65b9\u9762\u3002\u4f7f\u7528Cython\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u6210C\uff0c\u4ece\u800c\u52a0\u901f\u8fd0\u884c\u3002\u53e6\u4e00\u4e2a\u9009\u62e9\u662fPyPy\uff0c\u8fd9\u662f\u4e00\u4e2a\u66ff\u4ee3\u7684Python\u89e3\u91ca\u5668\uff0c\u80fd\u591f\u901a\u8fc7\u5373\u65f6\u7f16\u8bd1\u6280\u672f\u663e\u8457\u63d0\u9ad8\u8fd0\u884c\u901f\u5ea6\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Profiling\u5de5\u5177\u5206\u6790Python\u4ee3\u7801\u7684\u6027\u80fd\u74f6\u9888\uff1f<\/strong><br \/>Profiling\u5de5\u5177\u80fd\u5e2e\u52a9\u5f00\u53d1\u8005\u8bc6\u522b\u4ee3\u7801\u4e2d\u7684\u6027\u80fd\u74f6\u9888\u3002\u4f7f\u7528cProfile\u6a21\u5757\u53ef\u4ee5\u6536\u96c6\u51fd\u6570\u8c03\u7528\u7684\u7edf\u8ba1\u4fe1\u606f\uff0c\u4ece\u800c\u4e86\u89e3\u6bcf\u4e2a\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u3002\u901a\u8fc7\u5206\u6790\u8fd9\u4e9b\u6570\u636e\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u627e\u51fa\u8017\u65f6\u8f83\u957f\u7684\u90e8\u5206\uff0c\u5e76\u8fdb\u884c\u9488\u5bf9\u6027\u7684\u4f18\u5316\u3002\u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u5982SnakeViz\uff0c\u53ef\u4ee5\u5c06cProfile\u7684\u8f93\u51fa\u8f6c\u5316\u4e3a\u56fe\u5f62\u5316\u754c\u9762\uff0c\u65b9\u4fbf\u5206\u6790\u548c\u7406\u89e3\u4ee3\u7801\u7684\u6267\u884c\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u8ba9Python\u8fd0\u884c\u66f4\u5feb\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f18\u5316\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u3001\u5229\u7528\u591a\u7ebf\u7a0b [&hellip;]","protected":false},"author":3,"featured_media":1172470,"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\/1172465"}],"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=1172465"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172465\/revisions"}],"predecessor-version":[{"id":1172471,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172465\/revisions\/1172471"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1172470"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1172465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1172465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1172465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}