{"id":50033,"date":"2023-09-24T22:33:00","date_gmt":"2022-11-08T11:03:36","guid":{"rendered":"https:\/\/www.silicloud.com\/zh\/blog\/%e3%80%90django%e3%80%91%e4%bd%bf%e7%94%a8django%e8%b0%83%e7%94%a8line-profiler\/"},"modified":"2024-05-04T01:24:46","modified_gmt":"2024-05-03T17:24:46","slug":"%e3%80%90django%e3%80%91%e4%bd%bf%e7%94%a8django%e8%b0%83%e7%94%a8line-profiler","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/zh\/blog\/%e3%80%90django%e3%80%91%e4%bd%bf%e7%94%a8django%e8%b0%83%e7%94%a8line-profiler\/","title":{"rendered":"\u3010Django\u3011\u4f7f\u7528Django\u8c03\u7528Line Profiler"},"content":{"rendered":"<h2>Line Profiler\u662f\u4ec0\u4e48\uff1f<\/h2>\n<p>\u8fd9\u662f\u4e00\u4e2a\u53ef\u4ee5\u6309\u884c\u8fdb\u884c\u5206\u6790\u7684\u6a21\u5757\u3002\u672c\u6b21\u6211\u5c06\u5728Django\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6d4b\u91cf\u6bcf\u884c\u7684\u5904\u7406\u65f6\u95f4\uff0c\u5e76\u5c06\u5176\u663e\u793a\u5728\u63a7\u5236\u53f0\u4e0a\u3002<\/p>\n<h2>Line Profiler\u7684\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<h3>1. \u5b89\u88c5Line Profiler<\/h3>\n<pre class=\"post-pre\"><code><span class=\"gp\">$<\/span> pip <span class=\"nb\">install <\/span>line_profiler\r\n<\/code><\/pre>\n<p>\u5b89\u88c5\u540e\uff0c\u6700\u597d\u4f7f\u7528 &#8220;$ pip freeze &gt; requirements.txt&#8221; \u8fd9\u4e2a\u547d\u4ee4\uff0c\u8fd9\u6837\u505a\u662f\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<h3>\u5728\u5e0c\u671b\u6d4b\u91cf\u5904\u7406\u65f6\u95f4\u7684\u6587\u4ef6\u4e2d\uff08\u4f8b\u5982views.py\uff09\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\u3002<\/h3>\n<pre class=\"post-pre\"><code><span class=\"c1\"># Line Profiler\r\n<\/span><span class=\"kn\">import<\/span> <span class=\"n\">line_profiler<\/span>\r\n<span class=\"kn\">import<\/span> <span class=\"n\">atexit<\/span>\r\n\r\n<span class=\"n\">profile<\/span> <span class=\"o\">=<\/span> <span class=\"n\">line_profiler<\/span><span class=\"p\">.<\/span><span class=\"nc\">LineProfiler<\/span><span class=\"p\">()<\/span>\r\n<span class=\"n\">atexit<\/span><span class=\"p\">.<\/span><span class=\"nf\">register<\/span><span class=\"p\">(<\/span><span class=\"n\">profile<\/span><span class=\"p\">.<\/span><span class=\"n\">print_stats<\/span><span class=\"p\">)<\/span>\r\n\r\n<span class=\"c1\"># \u6e2c\u5b9a\u3057\u305f\u3044\u95a2\u6570\u306b\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4ed8\u3051\u308b\r\n<\/span><span class=\"nd\">@profile<\/span>\r\n<span class=\"k\">def<\/span> <span class=\"nf\">foo<\/span><span class=\"p\">():<\/span>\r\n    <span class=\"bp\">...<\/span>\r\n<\/code><\/pre>\n<h3>3. \u8fd0\u884c\u670d\u52a1\u5668<\/h3>\n<pre class=\"post-pre\"><code><span class=\"gp\">$<\/span> python manage.py runserver\r\n<\/code><\/pre>\n<h3>\u66f4\u65b0\u6d4f\u89c8\u5668\u540e\u6267\u884c\u51fd\u6570\u3002<\/h3>\n<h3>\uff15. \u4f7f\u7528 Ctrl+c \u505c\u6b62 runserver \u7684\u8fd0\u884c\u3002<\/h3>\n<p>\u6d4b\u91cf\u7ed3\u679c\u5c06\u5728\u63a7\u5236\u53f0\u4e0a\u663e\u793a\u2193<\/p>\n<pre class=\"post-pre\"><code><span class=\"gp\">Line #<\/span>      Hits         Time  Per Hit   % Time  Line Contents\r\n<span class=\"go\">==============================================================\r\n     3                                           @profile\r\n     4                                           def foo():\r\n     5         1    1557939.0      0.3     50.9      print('Hello World')\r\n     6         1   10541423.0      2.1     49.1      print('foo')\r\n<\/span><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Line Profiler\u662f\u4ec0\u4e48\uff1f \u8fd9\u662f\u4e00\u4e2a\u53ef\u4ee5\u6309\u884c\u8fdb\u884c\u5206\u6790\u7684\u6a21\u5757\u3002\u672c\u6b21\u6211\u5c06\u5728Django\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6d4b\u91cf\u6bcf\u884c\u7684 [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-50033","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010Django\u3011\u4f7f\u7528Django\u8c03\u7528Line Profiler - Blog - Silicon Cloud<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/zh\/blog\/\u3010django\u3011\u4f7f\u7528django\u8c03\u7528line-profiler\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010Django\u3011\u4f7f\u7528Django\u8c03\u7528Line Profiler\" \/>\n<meta property=\"og:description\" content=\"Line Profiler\u662f\u4ec0\u4e48\uff1f \u8fd9\u662f\u4e00\u4e2a\u53ef\u4ee5\u6309\u884c\u8fdb\u884c\u5206\u6790\u7684\u6a21\u5757\u3002\u672c\u6b21\u6211\u5c06\u5728Django\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6d4b\u91cf\u6bcf\u884c\u7684 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/zh\/blog\/\u3010django\u3011\u4f7f\u7528django\u8c03\u7528line-profiler\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-08T11:03:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-05-03T17:24:46+00:00\" \/>\n<meta name=\"author\" content=\"\u6e05, \u5b87\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u6e05, \u5b87\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e3%80%90django%e3%80%91%e4%bd%bf%e7%94%a8django%e8%b0%83%e7%94%a8line-profiler\/\",\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e3%80%90django%e3%80%91%e4%bd%bf%e7%94%a8django%e8%b0%83%e7%94%a8line-profiler\/\",\"name\":\"\u3010Django\u3011\u4f7f\u7528Django\u8c03\u7528Line Profiler - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#website\"},\"datePublished\":\"2022-11-08T11:03:36+00:00\",\"dateModified\":\"2024-05-03T17:24:46+00:00\",\"author\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/1a6ecd3d914d22a5ac32791ffc1fbd8e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e3%80%90django%e3%80%91%e4%bd%bf%e7%94%a8django%e8%b0%83%e7%94%a8line-profiler\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/zh\/blog\/%e3%80%90django%e3%80%91%e4%bd%bf%e7%94%a8django%e8%b0%83%e7%94%a8line-profiler\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e3%80%90django%e3%80%91%e4%bd%bf%e7%94%a8django%e8%b0%83%e7%94%a8line-profiler\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.silicloud.com\/zh\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Django\u3011\u4f7f\u7528Django\u8c03\u7528Line Profiler\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/\",\"name\":\"Blog - Silicon Cloud\",\"description\":\"\",\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/1a6ecd3d914d22a5ac32791ffc1fbd8e\",\"name\":\"\u6e05, \u5b87\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4b2016c18459a605fc469c7566608f5686491baa112d0871ee613f61b7210565?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4b2016c18459a605fc469c7566608f5686491baa112d0871ee613f61b7210565?s=96&d=mm&r=g\",\"caption\":\"\u6e05, \u5b87\"},\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/author\/qingyu\/\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e3%80%90django%e3%80%91%e4%bd%bf%e7%94%a8django%e8%b0%83%e7%94%a8line-profiler\/#local-main-organization-logo\",\"url\":\"\",\"contentUrl\":\"\",\"caption\":\"Blog - Silicon Cloud\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"\u3010Django\u3011\u4f7f\u7528Django\u8c03\u7528Line Profiler - Blog - Silicon Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/zh\/blog\/\u3010django\u3011\u4f7f\u7528django\u8c03\u7528line-profiler\/","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Django\u3011\u4f7f\u7528Django\u8c03\u7528Line Profiler","og_description":"Line Profiler\u662f\u4ec0\u4e48\uff1f \u8fd9\u662f\u4e00\u4e2a\u53ef\u4ee5\u6309\u884c\u8fdb\u884c\u5206\u6790\u7684\u6a21\u5757\u3002\u672c\u6b21\u6211\u5c06\u5728Django\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6d4b\u91cf\u6bcf\u884c\u7684 [&hellip;]","og_url":"https:\/\/www.silicloud.com\/zh\/blog\/\u3010django\u3011\u4f7f\u7528django\u8c03\u7528line-profiler\/","og_site_name":"Blog - Silicon Cloud","article_published_time":"2022-11-08T11:03:36+00:00","article_modified_time":"2024-05-03T17:24:46+00:00","author":"\u6e05, \u5b87","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"\u6e05, \u5b87"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e3%80%90django%e3%80%91%e4%bd%bf%e7%94%a8django%e8%b0%83%e7%94%a8line-profiler\/","url":"https:\/\/www.silicloud.com\/zh\/blog\/%e3%80%90django%e3%80%91%e4%bd%bf%e7%94%a8django%e8%b0%83%e7%94%a8line-profiler\/","name":"\u3010Django\u3011\u4f7f\u7528Django\u8c03\u7528Line Profiler - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/#website"},"datePublished":"2022-11-08T11:03:36+00:00","dateModified":"2024-05-03T17:24:46+00:00","author":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/1a6ecd3d914d22a5ac32791ffc1fbd8e"},"breadcrumb":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e3%80%90django%e3%80%91%e4%bd%bf%e7%94%a8django%e8%b0%83%e7%94%a8line-profiler\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/zh\/blog\/%e3%80%90django%e3%80%91%e4%bd%bf%e7%94%a8django%e8%b0%83%e7%94%a8line-profiler\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e3%80%90django%e3%80%91%e4%bd%bf%e7%94%a8django%e8%b0%83%e7%94%a8line-profiler\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.silicloud.com\/zh\/blog\/"},{"@type":"ListItem","position":2,"name":"\u3010Django\u3011\u4f7f\u7528Django\u8c03\u7528Line Profiler"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#website","url":"https:\/\/www.silicloud.com\/zh\/blog\/","name":"Blog - Silicon Cloud","description":"","inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/1a6ecd3d914d22a5ac32791ffc1fbd8e","name":"\u6e05, \u5b87","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4b2016c18459a605fc469c7566608f5686491baa112d0871ee613f61b7210565?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b2016c18459a605fc469c7566608f5686491baa112d0871ee613f61b7210565?s=96&d=mm&r=g","caption":"\u6e05, \u5b87"},"url":"https:\/\/www.silicloud.com\/zh\/blog\/author\/qingyu\/"},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e3%80%90django%e3%80%91%e4%bd%bf%e7%94%a8django%e8%b0%83%e7%94%a8line-profiler\/#local-main-organization-logo","url":"","contentUrl":"","caption":"Blog - Silicon Cloud"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/50033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/comments?post=50033"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/50033\/revisions"}],"predecessor-version":[{"id":95273,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/50033\/revisions\/95273"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/media?parent=50033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/categories?post=50033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/tags?post=50033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}