{"id":301397,"date":"2024-05-20T18:07:10","date_gmt":"2024-05-20T10:07:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/301397.html"},"modified":"2024-05-20T18:07:37","modified_gmt":"2024-05-20T10:07:37","slug":"python-%e7%a8%8b%e5%ba%8f%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-cprofile-%e5%b7%a5%e5%85%b7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/301397.html","title":{"rendered":"python \u7a0b\u5e8f\u5982\u4f55\u4f7f\u7528 cProfile \u5de5\u5177"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26213617\/b791e93a-61fc-41c0-a7e3-bc41701d7e16.webp\" alt=\"python \u7a0b\u5e8f\u5982\u4f55\u4f7f\u7528 cProfile \u5de5\u5177\" \/><\/p>\n<p><p>Python \u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528 cProfile \u5de5\u5177\u6765\u6267\u884c\u6027\u80fd\u5206\u6790\u3001<strong>\u76d1\u6d4b\u4ee3\u7801\u6027\u80fd<\/strong>\u548c<strong>\u627e\u51fa\u74f6\u9888<\/strong>\u3002\u4f7f\u7528 cProfile \u7684\u57fa\u672c\u65b9\u6cd5\u5305\u62ec\u547d\u4ee4\u884c\u8c03\u7528\u548c\u4ee3\u7801\u5185\u8c03\u7528\u3002\u5728\u547d\u4ee4\u884c\u4e2d\uff0c\u53ef\u901a\u8fc7\u7b80\u5355\u7684\u53c2\u6570 <code>-m cProfile<\/code> \u540e\u63a5\u7a0b\u5e8f\u6587\u4ef6\u540d\u6765\u542f\u52a8\u6027\u80fd\u5206\u6790\u3002\u800c\u5728\u4ee3\u7801\u4e2d\uff0c\u53ef\u901a\u8fc7\u5bfc\u5165 cProfile \u6a21\u5757\u3001\u4f7f\u7528<code>cProfile.run()<\/code>\u6216\u521b\u5efa<code>cProfile.Profile<\/code> \u7c7b\u7684\u5b9e\u4f8b\u6765\u63a7\u5236\u5206\u6790\u8fc7\u7a0b\u3002<strong>\u638c\u63e1 cProfile \u7684\u4f7f\u7528\u6709\u52a9\u4e8e\u4f18\u5316\u4ee3\u7801\u6267\u884c\u6548\u7387<\/strong>\uff0c\u5b83\u901a\u8fc7\u7edf\u8ba1\u51fd\u6570\u8c03\u7528\u6b21\u6570\u3001\u8fd0\u884c\u65f6\u95f4\u7b49\u4fe1\u606f\u5e2e\u52a9\u5f00\u53d1\u8005\u4e86\u89e3\u7a0b\u5e8f\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001CPROFILE\u7b80\u4ecb\u4e0e\u5b89\u88c5<\/h3>\n<\/p>\n<p><p>cProfile \u662f Python \u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u4e00\u822c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002\u5b83\u662f\u4e00\u4e2a\u6027\u80fd\u5206\u6790\u5668\uff0c\u53ef\u4ee5\u6536\u96c6\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7684\u6027\u80fd\u6570\u636e\uff0c\u5305\u62ec\u51fd\u6570\u8c03\u7528\u6b21\u6570\u3001\u8fd0\u884c\u65f6\u95f4\u3001\u9012\u5f52\u8c03\u7528\u60c5\u51b5\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e3a\u4f7f\u7528 cProfile\uff0c\u9996\u5148\u8981\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u5df2\u5b89\u88c5<\/strong>\u3002\u5927\u591a\u6570Python\u53d1\u884c\u7248\u5df2\u7ecf\u5185\u7f6e\u6b64\u6a21\u5757\uff0c\u53ef\u901a\u8fc7\u8fdb\u5165 Python \u73af\u5883\u5e76\u5c1d\u8bd5\u5bfc\u5165 cProfile \u6765\u8fdb\u884c\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cProfile<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u9047\u5230\u5bfc\u5165\u9519\u8bef\uff0c\u5219\u8868\u793a cProfile \u5df2\u51c6\u5907\u5c31\u7eea\uff0c\u53ef\u4ee5\u5f00\u59cb\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528CPROFILE\u8fdb\u884c\u547d\u4ee4\u884c\u6027\u80fd\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528 cProfile \u975e\u5e38\u76f4\u89c2\u3002\u4f60\u53ea\u9700\u8981\u5728\u6267\u884c Python \u811a\u672c\u65f6\uff0c\u5c06 <code>python<\/code> \u547d\u4ee4\u66ff\u6362\u4e3a <code>python -m cProfile<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5bf9\u6574\u4e2a\u7a0b\u5e8f\u8fdb\u884c\u6027\u80fd\u5206\u6790\u3002<\/p>\n<\/p>\n<p><p><strong>\u4ee5\u5982\u4e0b\u683c\u5f0f\u6267\u884c\u4f60\u7684Python\u811a\u672c<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">python -m cProfile my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>cProfile \u8fd8\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u9009\u9879<\/strong>\uff0c\u4f8b\u5982 <code>-o<\/code> \u7528\u4e8e\u6307\u5b9a\u8f93\u51fa\u6587\u4ef6\uff0c <code>-s<\/code> \u7528\u4e8e\u6307\u5b9a\u6392\u5e8f\u65b9\u5f0f\u3002 \u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u6309\u7167\u8fd0\u884c\u65f6\u95f4\u5bf9\u8f93\u51fa\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">python -m cProfile -s time my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5728\u4ee3\u7801\u4e2d\u96c6\u6210CPROFILE<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u5bf9\u7279\u5b9a\u90e8\u5206\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff0c\u53ef\u4ee5\u5bfc\u5165 cProfile \u5e76\u4f7f\u7528\u5176 API\u3002\u4ee5\u4e0b\u662f\u4e24\u79cd\u4e3b\u8981\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528cProfile.run()<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>cProfile.run()<\/code> \u51fd\u6570\uff0c\u5e76\u4f20\u9012\u9700\u8981\u8fd0\u884c\u7684\u4ee3\u7801\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u597d\u5904\u662f\u76f4\u63a5\u4e0e\u4ee3\u7801\u96c6\u6210\uff0c\u4fbf\u4e8e\u63a7\u5236\u5206\u6790\u7684\u8303\u56f4\u548c\u65f6\u673a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cProfile<\/p>\n<p>def function_to_profile():<\/p>\n<p>    # \u4f60\u7684\u4ee3\u7801\u903b\u8f91<\/p>\n<p>    pass<\/p>\n<p>cProfile.run(&#039;function_to_profile()&#039;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u548c\u4f7f\u7528Profile\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u53e6\u4e00\u4e2a\u9009\u62e9\u662f\u521b\u5efa\u4e00\u4e2a<code>cProfile.Profile<\/code>\u5bf9\u8c61\uff0c\u8fd9\u5141\u8bb8\u66f4\u7ec6\u7c92\u5ea6\u7684\u63a7\u5236\uff0c\u540c\u65f6\u4f60\u4e5f\u53ef\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u6765\u5904\u7406\u5206\u6790\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cProfile<\/p>\n<p>def function_to_profile():<\/p>\n<p>    # \u4f60\u7684\u4ee3\u7801\u903b\u8f91<\/p>\n<p>    pass<\/p>\n<p>profiler = cProfile.Profile()<\/p>\n<p>profiler.enable()  # \u5f00\u59cb\u6536\u96c6\u6027\u80fd\u6570\u636e<\/p>\n<p>function_to_profile()<\/p>\n<p>profiler.disable()  # \u505c\u6b62\u6536\u96c6\u6027\u80fd\u6570\u636e<\/p>\n<p>profiler.print_stats(sort=&#039;time&#039;)  # \u6253\u5370\u5206\u6790\u7ed3\u679c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5206\u6790CPROFILE\u8f93\u51fa\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>cProfile \u8f93\u51fa\u7684\u6027\u80fd\u5206\u6790\u62a5\u544a\u5305\u542b\u4e86\u591a\u5217\u6570\u636e\uff0c\u4e3b\u8981\u5305\u62ec\u8c03\u7528\u6b21\u6570(ncalls)\u3001\u603b\u8fd0\u884c\u65f6\u95f4(tottime)\u3001\u5355\u6b21\u8fd0\u884c\u65f6\u95f4(percall)\u3001\u7d2f\u79ef\u65f6\u95f4(cumtime)\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u6bcf\u5217\u6570\u636e\u7684\u542b\u4e49\u5982\u4e0b<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>ncalls<\/code>: \u51fd\u6570\u7684\u88ab\u8c03\u7528\u6b21\u6570\u3002<\/li>\n<li><code>tottime<\/code>: \u51fd\u6570\u672c\u8eab\u8fd0\u884c\u65f6\u95f4\u7684\u603b\u548c\uff0c\u4e0d\u5305\u62ec\u8c03\u7528\u5176\u4ed6\u51fd\u6570\u7684\u65f6\u95f4\u3002<\/li>\n<li><code>percall<\/code>: <code>tottime<\/code>\u9664\u4ee5<code>ncalls<\/code>\u7684\u7ed3\u679c\uff0c\u5373\u51fd\u6570\u6bcf\u6b21\u8c03\u7528\u7684\u5e73\u5747\u8fd0\u884c\u65f6\u95f4\u3002<\/li>\n<li><code>cumtime<\/code>: \u51fd\u6570\u53ca\u5176\u6240\u6709\u5b50\u51fd\u6570\u7684\u7d2f\u79ef\u8fd0\u884c\u65f6\u95f4\u7684\u603b\u548c\u3002<\/li>\n<li><code>percall<\/code>: <code>cumtime<\/code>\u9664\u4ee5\u51fd\u6570\u7684\u8c03\u7528\u6b21\u6570\u3002<\/li>\n<\/ul>\n<p><p><strong>\u5206\u6790\u6027\u80fd\u6570\u636e\u65f6\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5173\u6ce8\u90a3\u4e9b\u603b\u8fd0\u884c\u65f6\u95f4\uff08<code>tottime<\/code>\uff09\u9ad8\u7684\u51fd\u6570\uff0c\u5b83\u4eec\u53ef\u80fd\u662f\u6027\u80fd\u74f6\u9888\u6240\u5728\u3002<\/li>\n<li>\u67e5\u770b<code>cumtime<\/code>\uff0c\u4e86\u89e3\u51fd\u6570\u53ca\u5176\u5b50\u51fd\u6570\u7684\u603b\u8fd0\u884c\u65f6\u95f4\uff0c\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u4f60\u4e86\u89e3\u65f6\u95f4\u662f\u5982\u4f55\u5728\u8c03\u7528\u94fe\u4e2d\u5206\u914d\u7684\u3002<\/li>\n<li>\u51fd\u6570\u88ab\u8c03\u7528\u6b21\u6570\u4e5f\u5f88\u91cd\u8981\uff0c\u6709\u65f6\u5019\u4f18\u5316\u8c03\u7528\u6b21\u6570\u53ef\u4ee5\u663e\u8457\u6539\u5584\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001CPROFILE\u6027\u80fd\u4f18\u5316\u5b9e\u6218<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528cProfile\u5f97\u5230\u6027\u80fd\u6570\u636e\u540e\uff0c\u4f60\u9700\u8981\u4f9d\u636e\u8fd9\u4e9b\u6570\u636e\u6765\u4f18\u5316\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u5316\u6b65\u9aa4\u4e00\u822c\u5305\u62ec<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u8bc6\u522b\u90a3\u4e9b\u5360\u7528\u6700\u591a\u65f6\u95f4\u7684\u51fd\u6570\u3002<\/li>\n<li>\u68c0\u67e5\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u7b97\u6cd5\u4f18\u5316\u6765\u51cf\u5c11\u8fd9\u4e9b\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u3002<\/li>\n<li>\u8003\u8651\u4f7f\u7528\u7f13\u5b58\u6765\u4f18\u5316\u91cd\u590d\u8ba1\u7b97\u3002<\/li>\n<li>\u68c0\u67e5\u51fd\u6570\u8c03\u7528\u6b21\u6570\uff0c\u770b\u662f\u5426\u53ef\u4ee5\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8c03\u7528\u3002<\/li>\n<\/ul>\n<p><p><strong>\u4ee5\u4e0b\u662f\u5b9e\u9645\u6848\u4f8b<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u6027\u80fd\u95ee\u9898\u7684\u590d\u6742\u8ba1\u7b97\u51fd\u6570\u3002\u901a\u8fc7cProfile\u5b9a\u4f4d\u5230\u8fd9\u4e2a\u51fd\u6570\uff0c\u4f60\u53d1\u73b0\u5b83\u88ab\u8c03\u7528\u4e86\u6210\u5343\u4e0a\u4e07\u6b21\u3002<\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5c1d\u8bd5\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u7528\u66f4\u6709\u6548\u7684\u7b97\u6cd5\u6765\u66ff\u6362\u5f53\u524d\u7684\u5b9e\u73b0\u3002<\/li>\n<li>\u5b9e\u73b0\u7f13\u5b58\u673a\u5236\uff0c\u6bd4\u5982\u4f7f\u7528 <code>functools.lru_cache<\/code> \u6765\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002<\/li>\n<li>\u91cd\u65b0\u8bbe\u8ba1\u51fd\u6570\u8c03\u7528\u7684\u903b\u8f91\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8c03\u7528\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u7ed3\u5408\u5176\u4ed6\u5de5\u5177\u4f7f\u7528CPROFILE<\/h3>\n<\/p>\n<p><p>cProfile \u7684\u8f93\u51fa\u53ef\u4ee5\u4e0e\u5176\u4ed6\u5de5\u5177\u7ed3\u5408\u8d77\u6765\uff0c\u4ee5\u63d0\u4f9b\u66f4\u53cb\u597d\u7684\u5206\u6790\u4f53\u9a8c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>pstats<\/code> \u6a21\u5757\u6765\u5206\u6790 <code>cProfile<\/code> \u8f93\u51fa\u7684\u6570\u636e\uff0c\u6216\u8005\u4f7f\u7528 <code>SnakeViz<\/code>\u3001<code>Py-Spy<\/code> \u7b49\u53ef\u89c6\u5316\u5de5\u5177\u6765\u66f4\u76f4\u89c2\u5730\u67e5\u770b\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06cProfile\u4e0e\u8fd9\u4e9b\u5de5\u5177\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u6027\u80fd\u5206\u6790\u548c\u8c03\u8bd5\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6027\u80fd\u5206\u6790\u65f6\uff0c<strong>\u4e00\u5b9a\u8981\u786e\u4fdd\u6d4b\u8bd5\u73af\u5883\u4e0e\u751f\u4ea7\u73af\u5883\u5c3d\u53ef\u80fd\u4e00\u81f4<\/strong>\uff0c\u56e0\u4e3a\u786c\u4ef6\u914d\u7f6e\u3001\u64cd\u4f5c\u7cfb\u7edf\u7b49\u56e0\u7d20\u90fd\u53ef\u80fd\u5f71\u54cd\u7a0b\u5e8f\u7684\u6027\u80fd\u8868\u73b0\u3002\u6b64\u5916\uff0c\u5c3d\u91cf\u5728\u6ca1\u6709\u5176\u4ed6\u8d1f\u8f7d\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\uff0c\u4ee5\u907f\u514d\u5916\u90e8\u56e0\u7d20\u7684\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><p>cProfile \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u4f46\u6700\u91cd\u8981\u7684\u662f\u5982\u4f55\u89e3\u8bfb\u5b83\u63d0\u4f9b\u7684\u6570\u636e\uff0c\u5e76\u7ed3\u5408\u7a0b\u5e8f\u903b\u8f91\u548c\u53ef\u80fd\u7684\u4f18\u5316\u7b56\u7565\u6765\u51b3\u5b9a\u5982\u4f55\u884c\u52a8\u3002\u6700\u7ec8\u76ee\u6807\u662f\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u63d0\u4f9b\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002\u4e0d\u8981\u8fc7\u5ea6\u4f18\u5316\u4ee3\u7801\u7684\u67d0\u4e00\u5c0f\u90e8\u5206\u800c\u5ffd\u7565\u4e86\u6574\u4f53\u7684\u6027\u80fd\u72b6\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5408\u7406\u4f7f\u7528cProfile\uff0c\u4f60\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u5347Python\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u4e3a\u7528\u6237\u63d0\u4f9b\u66f4\u52a0\u6d41\u7545\u548c\u9ad8\u6548\u7684\u8fd0\u884c\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528 cProfile \u5de5\u5177\u6765\u5206\u6790 Python \u7a0b\u5e8f\u7684\u6027\u80fd?<\/strong><\/p>\n<p>\u4f7f\u7528 cProfile \u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5206\u6790 Python \u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u4ee5\u4e0b\u662f\u4f7f\u7528 cProfile \u7684\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\n<p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5bfc\u5165 cProfile \u6a21\u5757\uff0c\u5e76\u5728\u4f60\u7684\u4ee3\u7801\u4e2d\u63d2\u5165 profile \u8bed\u53e5\u3002<\/p>\n<\/li>\n<li>\n<p>\u63a5\u4e0b\u6765\uff0c\u4f60\u53ef\u4ee5\u8fd0\u884c\u7a0b\u5e8f\u5e76\u8bb0\u5f55\u6027\u80fd\u6570\u636e\u3002\u6307\u5b9a\u8f93\u51fa\u6587\u4ef6\u540d\uff0c\u4f7f\u7528\u547d\u4ee4 <code>python -m cProfile -o outputfile.py &lt;your_program.py&gt;<\/code>\u7ecf\u8fc7\u4e00\u6bb5\u65f6\u95f4\u540e\uff0c\u7a0b\u5e8f\u8fd0\u884c\u5b8c\u6bd5\u3002<\/p>\n<\/li>\n<li>\n<p>\u6700\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 pstats \u6a21\u5757\u6765\u5206\u6790\u6027\u80fd\u6570\u636e\u3002\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4 <code>python -m pstats outputfile.py<\/code>\uff0c\u7136\u540e\u53ef\u4ee5\u4f7f\u7528 <code>strip_dirs()<\/code> \u65b9\u6cd5\u6765\u53bb\u9664\u6587\u4ef6\u8def\u5f84\u4fe1\u606f\uff0c\u4f7f\u7528 <code>sort_stats()<\/code> \u65b9\u6cd5\u6765\u6309\u7167\u81ea\u5df1\u7684\u9700\u6c42\u6392\u5e8f\uff0c\u5e76\u4f7f\u7528 <code>print_stats()<\/code> \u65b9\u6cd5\u6765\u6253\u5370\u6027\u80fd\u6570\u636e\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>2. cProfile \u5de5\u5177\u5bf9\u4e8e\u4f18\u5316 Python \u7a0b\u5e8f\u6709\u4f55\u5e2e\u52a9?<\/strong><\/p>\n<p>cProfile \u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u627e\u51fa Python \u7a0b\u5e8f\u4e2d\u7684\u6027\u80fd\u74f6\u9888\uff0c\u4ece\u800c\u8fdb\u884c\u9488\u5bf9\u6027\u7684\u4f18\u5316\u3002\u901a\u8fc7\u5206\u6790 cProfile \u751f\u6210\u7684\u6027\u80fd\u6570\u636e\uff0c\u4f60\u53ef\u4ee5\u53d1\u73b0\u7a0b\u5e8f\u4e2d\u8017\u8d39\u65f6\u95f4\u6700\u591a\u7684\u51fd\u6570\u6216\u65b9\u6cd5\uff0c\u4ee5\u53ca\u8c03\u7528\u5b83\u4eec\u7684\u4e0a\u4e0b\u6587\u3002<\/p>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u6709\u9488\u5bf9\u6027\u5730\u4f18\u5316\u8fd9\u4e9b\u51fd\u6570\u6216\u65b9\u6cd5\uff0c\u4ece\u800c\u63d0\u5347\u6574\u4f53\u7a0b\u5e8f\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5bf9\u8017\u65f6\u957f\u7684\u51fd\u6570\u8fdb\u884c\u4ee3\u7801\u91cd\u6784\uff0c\u6216\u8005\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u7b97\u6cd5\u66ff\u4ee3\u4f4e\u6548\u7684\u5b9e\u73b0\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u89e3\u8bfb cProfile \u751f\u6210\u7684\u6027\u80fd\u6570\u636e?<\/strong><\/p>\n<p>cProfile \u751f\u6210\u7684\u6027\u80fd\u6570\u636e\u4e2d\u6709\u51e0\u4e2a\u5173\u952e\u7684\u6307\u6807\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>\n<p><code>ncalls<\/code> \u6307\u6807\u8868\u793a\u51fd\u6570\u7684\u8c03\u7528\u6b21\u6570\u3002<\/p>\n<\/li>\n<li>\n<p><code>tottime<\/code> \u6307\u6807\u8868\u793a\u51fd\u6570\u8fd0\u884c\u7684\u603b\u65f6\u95f4\uff0c\u4e0d\u5305\u62ec\u51fd\u6570\u8c03\u7528\u7684\u65f6\u95f4\u3002<\/p>\n<\/li>\n<li>\n<p><code>percall<\/code> \u6307\u6807\u8868\u793a\u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u7684\u5e73\u5747\u65f6\u95f4\uff0c\u662f <code>tottime<\/code> \u9664\u4ee5 <code>ncalls<\/code> \u7684\u7ed3\u679c\u3002<\/p>\n<\/li>\n<li>\n<p><code>cumtime<\/code> \u6307\u6807\u8868\u793a\u51fd\u6570\u8fd0\u884c\u7684\u603b\u65f6\u95f4\uff0c\u5305\u62ec\u51fd\u6570\u8c03\u7528\u7684\u65f6\u95f4\u3002<\/p>\n<\/li>\n<li>\n<p><code>percall<\/code> \u6307\u6807\u8868\u793a\u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u7684\u5e73\u5747\u65f6\u95f4\uff0c\u662f <code>cumtime<\/code> \u9664\u4ee5 <code>ncalls<\/code> \u7684\u7ed3\u679c\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5982\u679c\u4f60\u60f3\u4e86\u89e3\u51fd\u6570\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u7684\u8fd0\u884c\u65f6\u95f4\u60c5\u51b5\uff0c\u53ef\u4ee5\u5173\u6ce8 <code>tottime<\/code> \u6216 <code>cumtime<\/code>\uff1b\u5982\u679c\u4f60\u60f3\u4e86\u89e3\u51fd\u6570\u7684\u6548\u7387\u60c5\u51b5\uff0c\u53ef\u4ee5\u53c2\u8003 <code>percall<\/code> \u7684\u6570\u503c\u3002\u901a\u8fc7\u5bf9\u8fd9\u4e9b\u6307\u6807\u7684\u5206\u6790\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u627e\u5230\u7a0b\u5e8f\u4e2d\u7684\u6027\u80fd\u95ee\u9898\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u4f18\u5316\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528 cProfile \u5de5\u5177\u6765\u6267\u884c\u6027\u80fd\u5206\u6790\u3001\u76d1\u6d4b\u4ee3\u7801\u6027\u80fd\u548c\u627e\u51fa\u74f6\u9888\u3002\u4f7f\u7528 cProfil [&hellip;]","protected":false},"author":3,"featured_media":301425,"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\/301397"}],"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=301397"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/301397\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/301425"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=301397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=301397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=301397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}