{"id":1652,"date":"2020-06-22T01:22:02","date_gmt":"2020-06-21T17:22:02","guid":{"rendered":"http:\/\/39.97.231.10\/?p=1652"},"modified":"2020-06-22T01:22:02","modified_gmt":"2020-06-21T17:22:02","slug":"%e4%bd%bf%e7%94%a8-python-%e7%9a%84-cprofile-%e6%a8%a1%e5%9d%97%e5%88%86%e6%9e%90%e4%bb%a3%e7%a0%81%e6%80%a7%e8%83%bd","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=1652","title":{"rendered":"\u4f7f\u7528 Python \u7684 cProfile \u6a21\u5757\u5206\u6790\u4ee3\u7801\u6027\u80fd"},"content":{"rendered":"<div>\n<div>\n<div>\n<div>cProfile \u6a21\u5757\u662f\u81ea python 2.5 \u4ee5\u6765\u6807\u51c6\u7248 Python \u89e3\u91ca\u5668\u7684\u9ed8\u8ba4\u6027\u80fd\u5206\u6790\u5668\u3002\u5b83\u662f\u4e00\u79cd\u786e\u5b9a\u6027\u5206\u6790\u5668\uff0c\u53ea\u6d4b\u91cf CPU \u65f6\u95f4\uff0c\u5e76\u4e0d\u5305\u542b\u5185\u5b58\u6d88\u8017\u548c\u5176\u4ed6\u4e0e\u5185\u5b58\u76f8\u5173\u8054\u7684\u4fe1\u606f\u3002<\/div>\n<\/div>\n<\/div>\n<\/div>\n<article>\u4ee3\u7801\u5206\u6790\u6a21\u677f\u5982\u4e0b\uff1a<\/p>\n<div>\n<pre>import cProfile, pstats, io\nfrom pstats import SortKey\npr = cProfile.Profile()\npr.enable()\n# ... do something ...\npr.disable()\ns = io.StringIO()\nsortby = SortKey.CUMULATIVE\nps = pstats.Stats(pr, stream=s).sort_stats(sortby)\nps.print_stats()\nprint(s.getvalue())\n<\/pre>\n<\/div>\n<p>\u6211\u4eec\u5728 do something \u6240\u5728\u884c\u5185\uff0c\u7f16\u5199\u9700\u8981\u6027\u80fd\u6d4b\u8bd5\u7684\u4ee3\u7801\u3002<br \/>\n\u8f93\u51fa\u7ed3\u679c\u5f62\u5982\uff1a<\/p>\n<div>\n<div>\n<div><\/div>\n<div data-width=\\\"1132\\\" data-height=\\\"149\\\"><img src=\\\"https:\/\/upload-images.jianshu.io\/upload_images\/3386108-519cd40404c8f064.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/1132\/format\/webp\\\" data-original-src=\\\"\/\/upload-images.jianshu.io\/upload_images\/3386108-519cd40404c8f064.png\\\" data-original-width=\\\"1132\\\" data-original-height=\\\"149\\\" data-original-format=\\\"image\/png\\\" data-original-filesize=\\\"16871\\\" data-image-index=\\\"0\\\" \/><\/div>\n<\/div>\n<\/div>\n<p>\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u9996\u5148\u5bfc\u5165\u76f8\u5173\u6a21\u5757\u3002<\/li>\n<li>\u521d\u5efa Profile \u5bf9\u8c61\u3002<\/li>\n<li>\u521d\u5efa IO \u5bf9\u8c61\uff0c\u7528\u4e8e\u5b58\u50a8\u5206\u6790\u7ed3\u679c\u3002<\/li>\n<li>\u8bbe\u5b9a\u5206\u6790\u7ed3\u679c\u6392\u5e8f\u5b57\u6bb5\u3002<\/li>\n<li>\u5f00\u59cb\u5206\u6790\u3002<\/li>\n<li>\u6253\u5370\u5206\u6790\u7ed3\u679c\u3002<\/li>\n<\/ol>\n<p>\u7edf\u8ba1\u7ed3\u679c\u5b57\u6bb5\u8bf4\u660e\u5982\u4e0b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u5b57\u6bb5<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>ncalls<\/td>\n<td>\u8c03\u7528\u6b21\u6570\u3002<\/td>\n<\/tr>\n<tr>\n<td>tottime<\/td>\n<td>\u5728\u6307\u5b9a\u51fd\u6570\u4e2d\u6d88\u8017\u7684\u603b\u65f6\u95f4\uff08\u4e0d\u5305\u62ec\u8c03\u7528\u5b50\u51fd\u6570\u7684\u65f6\u95f4\uff09\u3002<\/td>\n<\/tr>\n<tr>\n<td>percall<\/td>\n<td>tottime \u9664\u4ee5 ncalls \u7684\u5546\uff0c\u5373\u5e73\u5747\u65f6\u95f4\u3002<\/td>\n<\/tr>\n<tr>\n<td>filename:lineno(function)<\/td>\n<td>\u8be6\u60c5\u8bf4\u660e\uff0c\u5373\u8c03\u7528\u51fd\u6570\u6240\u5728\u6587\u4ef6\u540d\uff1a\u884c\u53f7\uff08\u51fd\u6570\u540d\uff09\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7528\u5230\u4e86 cProfile \u5bf9\u8c61\u4e2d\u7684\u8fd9\u4e9b\u65b9\u6cd5\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u65b9\u6cd5<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>enable()<\/td>\n<td>\u5f00\u59cb\u6536\u96c6\u5206\u6790\u6570\u636e\u3002<\/td>\n<\/tr>\n<tr>\n<td>disable()<\/td>\n<td>\u505c\u6b62\u6536\u96c6\u5206\u6790\u6570\u636e\u3002<\/td>\n<\/tr>\n<tr>\n<td>print_stats(sort=-1)<\/td>\n<td>\u6253\u5370\u5206\u6790\u7ed3\u679c\u5230\u8f93\u51fa\u6d41\uff0c\u9ed8\u8ba4\u4e3a\u968f\u673a\u6392\u5e8f\u5e8f\u5217\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5982\u679c\u7b2c\u4e00\u5217\u4e2d\u6709\u4e24\u4e2a\u6570\u5b57\uff08\u4f8b\u59823\/1\uff09\uff0c\u5219\u8868\u793a\u51fd\u6570\u9012\u5f52\u3002\u7b2c\u4e8c\u4e2a\u503c\u662f\u539f\u59cb\u8c03\u7528\u6b21\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u8c03\u7528\u7684\u603b\u6b21\u6570\u3002\u6ce8\u610f\uff0c\u5f53\u51fd\u6570\u4e0d\u9012\u5f52\u65f6\uff0c\u8fd9\u4e24\u4e2a\u503c\u662f\u76f8\u540c\u7684\uff0c\u5e76\u4e14\u53ea\u6253\u5370\u5355\u4e2a\u6570\u5b57\u3002<br \/>\npstats.Stats \u662f\u4e2a\u7c7b\uff0c\u5b9a\u4e49\u4e3a\uff1a<\/p>\n<div>\n<pre> pstats.Stats(*filenames or profile, stream=sys.stdout)\n<\/pre>\n<\/div>\n<p>\u7528\u4e8e\u521b\u5efa\u7edf\u8ba1\u5bf9\u8c61\uff0c\u63a5\u53d7\u4e24\u4e2a\u5165\u53c2\uff0c\u4ee3\u7801\u793a\u4f8b\u4e2d\u4e3a profile \u548c\u8f93\u51fa\u6d41\u3002<br \/>\npstats.Stats \u6709\u4e00\u4e2a sort_stats(*keys) \u65b9\u6cd5\uff0c\u7528\u4e8e\u6392\u5e8f\u7edf\u8ba1\u7ed3\u679c\uff0c\u652f\u6301\u4ee5\u4e0b\u6392\u5e8f\u53c2\u6570\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u6392\u5e8f\u53c2\u6570<\/th>\n<th>\u6709\u6548\u679a\u4e3e\u53c2\u6570<\/th>\n<th>\u542b\u4e49<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\\&#8217;calls\\&#8217;<\/td>\n<td>SortKey.CALLS<\/td>\n<td>\u8c03\u7528\u6b21\u6570<\/td>\n<\/tr>\n<tr>\n<td>\\&#8217;cumulative\\&#8217;<\/td>\n<td>SortKey.CUMULATIVE<\/td>\n<td>\u7d2f\u79ef\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td>\\&#8217;cumtime\\&#8217;<\/td>\n<td>N\/A<\/td>\n<td>\u7d2f\u79ef\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td>\\&#8217;file\\&#8217;<\/td>\n<td>N\/A<\/td>\n<td>\u6587\u4ef6\u540d<\/td>\n<\/tr>\n<tr>\n<td>\\&#8217;filename\\&#8217;<\/td>\n<td>SortKey.FILENAME<\/td>\n<td>\u6587\u4ef6\u540d<\/td>\n<\/tr>\n<tr>\n<td>\\&#8217;module\\&#8217;<\/td>\n<td>N\/A<\/td>\n<td>\u6587\u4ef6\u540d<\/td>\n<\/tr>\n<tr>\n<td>\\&#8217;ncalls\\&#8217;<\/td>\n<td>N\/A<\/td>\n<td>\u8c03\u7528\u6b21\u6570<\/td>\n<\/tr>\n<tr>\n<td>\\&#8217;pcalls\\&#8217;<\/td>\n<td>SortKey.PCALLS<\/td>\n<td>\u539f\u59cb\u8c03\u7528\u8ba1\u6570<\/td>\n<\/tr>\n<tr>\n<td>\\&#8217;line\\&#8217;<\/td>\n<td>SortKey.LINE<\/td>\n<td>\u884c\u53f7<\/td>\n<\/tr>\n<tr>\n<td>\\&#8217;name\\&#8217;<\/td>\n<td>SortKey.NAME<\/td>\n<td>\u51fd\u6570\u540d\u79f0<\/td>\n<\/tr>\n<tr>\n<td>\\&#8217;nfl\\&#8217;<\/td>\n<td>SortKey.NFL<\/td>\n<td>\u540d\u79f0\/\u6587\u4ef6\/\u884c<\/td>\n<\/tr>\n<tr>\n<td>\\&#8217;stdname\\&#8217;<\/td>\n<td>SortKey.STDNAME<\/td>\n<td>\u6807\u51c6\u540d\u79f0<\/td>\n<\/tr>\n<tr>\n<td>\\&#8217;time\\&#8217;<\/td>\n<td>SortKey.TIME<\/td>\n<td>\u5185\u90e8\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td>\\&#8217;tottime\\&#8217;<\/td>\n<td>N\/A<\/td>\n<td>\u5185\u90e8\u65f6\u95f4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>cProfile \u6a21\u5757\u662f\u81ea python 2.5 \u4ee5\u6765\u6807\u51c6\u7248 Python \u89e3\u91ca\u5668\u7684\u9ed8\u8ba4\u6027\u80fd\u5206\u6790\u5668\u3002\u5b83\u662f\u4e00\u79cd\u786e &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=1652\"> <span class=\"screen-reader-text\">\u4f7f\u7528 Python \u7684 cProfile \u6a21\u5757\u5206\u6790\u4ee3\u7801\u6027\u80fd<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[106,126],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"cProfile \u6a21\u5757\u662f\u81ea python 2.5 \u4ee5\u6765\u6807\u51c6\u7248 Python \u89e3\u91ca\u5668\u7684\u9ed8\u8ba4\u6027\u80fd\u5206\u6790\u5668\u3002\u5b83\u662f\u4e00\u79cd\u786e&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1652"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1652"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1652\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1652"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}