{"id":1978,"date":"2015-03-07T21:39:43","date_gmt":"2015-03-07T13:39:43","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1978"},"modified":"2015-03-07T21:39:43","modified_gmt":"2015-03-07T13:39:43","slug":"python%e8%84%9a%e6%9c%ac%e7%9a%84%e6%80%a7%e8%83%bd%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1978.html","title":{"rendered":"Python\u811a\u672c\u7684\u6027\u80fd\u5206\u6790"},"content":{"rendered":"<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<p><a href=\"http:\/\/search.aol.com\/aol\/search?q=python+running+performance+analysis\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=python+running+performance+analysis<\/a><\/p>\n<h6>\u53c2\u8003\u7ed3\u8bba\uff1a<\/h6>\n<p>Python includes a profiler called cProfile. It not only gives the total running time, but also times each function separately, and tells you how many times each function was called, making it easy to determine where you should make optimizations.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>You can call it from within your code, or from the interpreter, like this<\/strong><\/span>:<\/p>\n<pre class=\"lang:default decode:true\">import cProfile\ncProfile.run('foo()')<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>Even more usefully, you can invoke the cProfile when running a script<\/strong><\/span>:<\/p>\n<pre class=\"lang:default decode:true\">$ python -m cProfile myscript.py<\/pre>\n<p>To make it even easier, I made a little batch file called &#8216;profile.bat&#8217;:<\/p>\n<pre class=\"lang:default decode:true\">python -m cProfile %1<\/pre>\n<p>So all I have to do is run:<\/p>\n<pre class=\"lang:default decode:true\">$ profile euler048.py<\/pre>\n<p>And I get this:<\/p>\n<pre class=\"lang:default decode:true\">1007 function calls in 0.061 CPU seconds\n\nOrdered by: standard name\nncalls  tottime  percall  cumtime  percall filename:lineno(function)\n    1    0.000    0.000    0.061    0.061 &lt;string&gt;:1(&lt;module&gt;)\n 1000    0.051    0.000    0.051    0.000 euler048.py:2(&lt;lambda&gt;)\n    1    0.005    0.005    0.061    0.061 euler048.py:2(&lt;module&gt;)\n    1    0.000    0.000    0.061    0.061 {execfile}\n    1    0.002    0.002    0.053    0.053 {map}\n    1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler objects}\n    1    0.000    0.000    0.000    0.000 {range}\n    1    0.003    0.003    0.003    0.003 {sum}<\/pre>\n<p>EDIT: Updated link to a good video resource from PyCon 2013: <a href=\"http:\/\/lanyrd.com\/2013\/pycon\/scdywg\/\" target=\"_blank\">http:\/\/lanyrd.com\/2013\/pycon\/scdywg\/<\/a><\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/582336\/how-can-you-profile-a-python-script\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/582336\/how-can-you-profile-a-python-script <\/a>\u00a0#\u7b80\u5355\u76f4\u63a5\uff01<\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/profile.html\" target=\"_blank\">https:\/\/docs.python.org\/2\/library\/profile.html<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/7069733\/how-do-i-read-the-output-of-the-ipython-prun-profiler-command\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/7069733\/how-do-i-read-the-output-of-the-ipython-prun-profiler-command<\/a><\/li>\n<li><a href=\"http:\/\/ymichael.com\/2014\/03\/08\/profiling-python-with-cprofile.html\" target=\"_blank\">http:\/\/ymichael.com\/2014\/03\/08\/profiling-python-with-cprofile.html<\/a><\/li>\n<li><a href=\"http:\/\/pymotw.com\/2\/profile\" target=\"_blank\">http:\/\/pymotw.com\/2\/profile<\/a><\/li>\n<li><a href=\"https:\/\/zapier.com\/engineering\/profiling-python-boss\/\" target=\"_blank\">https:\/\/zapier.com\/engineering\/profiling-python-boss\/<\/a><\/li>\n<li><a href=\"https:\/\/zapier.com\/engineering\/debugging-python-boss\/\" target=\"_blank\">https:\/\/zapier.com\/engineering\/debugging-python-boss\/ <\/a>\u00a0\u00a0#\u8c03\u8bd5Python\uff08\u4f46\u5404\u4eba\u66f4\u503e\u5411\u4e8eprint\uff09<\/li>\n<li><a href=\"http:\/\/www.huyng.com\/posts\/python-performance-analysis\/\" target=\"_blank\">http:\/\/www.huyng.com\/posts\/python-performance-analysis\/<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/110259\/which-python-memory-profiler-is-recommended\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/110259\/which-python-memory-profiler-is-recommended<\/a><\/li>\n<\/ul>\n<p>==<\/p>\n<h5>\u5982\u4f55\u68c0\u6d4bPython\u811a\u672c\u53ef\u80fd\u5b58\u5728\u7684\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\uff1f<\/h5>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<p><a href=\"http:\/\/search.aol.com\/aol\/search?q=python+memory+leakage\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=python+memory+leakage<\/a><\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/110259\/which-python-memory-profiler-is-recommended\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/110259\/which-python-memory-profiler-is-recommended<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/1641231\/python-working-around-memory-leaks\/\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/1641231\/python-working-around-memory-leaks\/<\/a><\/li>\n<li><a href=\"http:\/\/www.lshift.net\/blog\/2008\/11\/14\/tracing-python-memory-leaks\/\" target=\"_blank\">http:\/\/www.lshift.net\/blog\/2008\/11\/14\/tracing-python-memory-leaks\/<\/a><\/li>\n<li><a href=\"http:\/\/mg.pov.lt\/blog\/hunting-python-memleaks.html\" target=\"_blank\">http:\/\/mg.pov.lt\/blog\/hunting-python-memleaks.html<\/a><\/li>\n<li><a href=\"http:\/\/chase-seibert.github.io\/blog\/2013\/08\/03\/diagnosing-memory-leaks-python.html\" target=\"_blank\">http:\/\/chase-seibert.github.io\/blog\/2013\/08\/03\/diagnosing-memory-leaks-python.html<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/1435415\/python-memory-leaks\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/1435415\/python-memory-leaks<\/a><\/li>\n<li><a href=\"https:\/\/pythonhosted.org\/Pympler\/tutorials\/muppy_tutorial.html\" target=\"_blank\">https:\/\/pythonhosted.org\/Pympler\/tutorials\/muppy_tutorial.html<\/a><\/li>\n<li><a href=\"https:\/\/pythonhosted.org\/Pympler\/muppy.html\" target=\"_blank\">https:\/\/pythonhosted.org\/Pympler\/muppy.html<\/a><\/li>\n<\/ul>\n<p>==<\/p>\n<h5>\u5728Python\u4e2d\u6d4b\u8bd5\u67d0\u6bb5\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4<\/h5>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/search.aol.com\/aol\/search?q=python+measure+time+of+code+profile\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=python+measure+time+of+code+profile<\/a><\/li>\n<\/ul>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/7370801\/measure-time-elapsed-in-python\">http:\/\/stackoverflow.com\/questions\/7370801\/measure-time-elapsed-in-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/1685221\/accurately-measure-time-python-function-takes\">http:\/\/stackoverflow.com\/questions\/1685221\/accurately-measure-time-python-function-takes<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/profile.html\">https:\/\/docs.python.org\/2\/library\/profile.html<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/85451\/python-time-clock-vs-time-time-accuracy\">http:\/\/stackoverflow.com\/questions\/85451\/python-time-clock-vs-time-time-accuracy<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/1557571\/how-to-get-time-of-a-python-program-execution\">http:\/\/stackoverflow.com\/questions\/1557571\/how-to-get-time-of-a-python-program-execution<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/15707056\/get-time-of-execution-of-a-block-of-code-in-python-2-7\">http:\/\/stackoverflow.com\/questions\/15707056\/get-time-of-execution-of-a-block-of-code-in-python-2-7<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/8220801\/how-to-use-timeit-module\">http:\/\/stackoverflow.com\/questions\/8220801\/how-to-use-timeit-module<\/a><\/li>\n<li><a href=\"http:\/\/pymotw.com\/2\/timeit\/\">http:\/\/pymotw.com\/2\/timeit\/<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2.7\/library\/debug.html\">https:\/\/docs.python.org\/2.7\/library\/debug.html<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2.7\/library\/timeit.html\">https:\/\/docs.python.org\/2.7\/library\/timeit.html<\/a><\/li>\n<\/ul>\n<h6>\u6d4b\u8bd5\u4f8b\u5b50\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/4901523\/whats-a-faster-operation-re-match-search-or-str-find\">http:\/\/stackoverflow.com\/questions\/4901523\/whats-a-faster-operation-re-match-search-or-str-find<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/4172131\/create-random-list-of-integers-in-python\">http:\/\/stackoverflow.com\/questions\/4172131\/create-random-list-of-integers-in-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/5086430\/how-to-pass-parameters-of-a-function-when-using-timeit-timer\">http:\/\/stackoverflow.com\/questions\/5086430\/how-to-pass-parameters-of-a-function-when-using-timeit-timer<\/a><\/li>\n<\/ul>\n<h6>\u53c2\u8003\u7ed3\u8bba\uff1a<\/h6>\n<p>Given test.py:<\/p>\n<pre class=\"lang:default decode:true\">...\ndef InsertionSort():\n\t...\ndef TimSort():\n\t...<\/pre>\n<p>run timeit like this:<\/p>\n<pre class=\"lang:default decode:true\">% python -mtimeit -s'import test' 'test.InsertionSort()'\n% python -mtimeit -s'import test' 'test.TimSort()'<\/pre>\n<p>The following example shows how the Command-Line Interface can be used to compare three different expressions:<\/p>\n<pre class=\"lang:default decode:true\">$ python -m timeit '\"-\".join(str(n) for n in range(100))'\n10000 loops, best of 3: 40.3 usec per loop\n$ python -m timeit '\"-\".join([str(n) for n in range(100)])'\n10000 loops, best of 3: 33.4 usec per loop\n$ python -m timeit '\"-\".join(map(str, range(100)))'\n10000 loops, best of 3: 25.2 usec per loop<\/pre>\n<p>This can be achieved from the Python Interface with:<\/p>\n<pre class=\"lang:default decode:true\">&gt;&gt;&gt; import timeit\n&gt;&gt;&gt; timeit.timeit('\"-\".join(str(n) for n in range(100))', number=10000)\n0.8187260627746582\n&gt;&gt;&gt; timeit.timeit('\"-\".join([str(n) for n in range(100)])', number=10000)\n0.7288308143615723\n&gt;&gt;&gt; timeit.timeit('\"-\".join(map(str, range(100)))', number=10000)\n0.5858950614929199<\/pre>\n<p>Note however that timeit will automatically determine the number of repetitions only when the command-line interface is used. In the Examples section you can find more advanced examples.<\/p>\n<p><a href=\"https:\/\/docs.python.org\/2\/library\/timeit.html\" target=\"_blank\">https:\/\/docs.python.org\/2\/library\/timeit.html<\/a><\/p>\n<pre class=\"lang:default decode:true\">#!\/usr\/bin\/env python\n# coding=utf-8\nimport time\n\ndef elapse_time(func):\n    def wrapper(*args, **kwargs):\n        beg_ts = time.time()\n        func(*args, **kwargs)\n        end_ts = time.time()\n        print(\"elapsed time: %f\" % (end_ts - beg_ts))\n    return wrapper\n\n@elapse_time\ndef test():\n    for i in xrange(0, 10000):\n        pass\n\nif __name__ == \"__main__\":\n    test()\n####\ndef test():\n    \"\"\"Stupid test function\"\"\"\n    L = []\n    for i in range(100):\n        L.append(i)\n\nif __name__ == '__main__':\n    import timeit\n    print(timeit.timeit(\"test()\", setup=\"from __main__ import test\"))<\/pre>\n<p>####<\/p>\n<pre class=\"lang:default decode:true  \">$ python -m timeit -s 'text = \"sample string\"; char = \"g\"' 'char in text'\n10000000 loops, best of 3: 0.0877 usec per loop\n$ python -m timeit -s 'text = \"sample string\"; char = \"g\"' 'text.find(char)'\n1000000 loops, best of 3: 0.342 usec per loop\n#\n&gt;&gt;&gt; import timeit\n&gt;&gt;&gt; t = timeit.Timer('char in text', setup='text = \"sample string\"; char = \"g\"')\n&gt;&gt;&gt; t.timeit()\n0.3955516149999312\n&gt;&gt;&gt; t.repeat()\n[0.40193588800002544, 0.3960157959998014, 0.39594301399984033]\n#\nIn [2]: %timeit?\nUsage, in line mode:\n  %timeit [-n&lt;N&gt; -r&lt;R&gt; [-t|-c] -q -p&lt;P&gt; -o] statement\nor in cell mode:\n  %%timeit [-n&lt;N&gt; -r&lt;R&gt; [-t|-c] -q -p&lt;P&gt; -o] setup_code\n  code\n  code...\n#\n#==\u4e0d\u5efa\u8bae\u4f7f\u7528IPython\u7684 %time \u9b54\u672f\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4f1a\u6253\u5370\u51fa\u7ed3\u679c\uff0c\u9664\u975e\u4f60\u771f\u7684\u9700\u8981\u7684\u8bdd==\nIn [10]: %time?\nDocstring:\nTime execution of a Python statement or expression.\n\nExamples:\n  In [1]: %time 2**128\n  CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s\n  Wall time: 0.00\n  Out[1]: 340282366920938463463374607431768211456L\n\nIn [9]: %time \"-\".join(str(n) for n in range(100))\nWall time: 0 ns\nOut[9]: '0-1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26-27-28-29-30-31-32-33-34-35-36-37-38-39-40-41-42-43-44-45-46-47-48-49-50-51-52-53-54-55-56-57-58-59-60-61-62-63-64-65-66-67-68-69-70-71-72-73-74-75-76-77-78-79-80-81-82-83-84-85-86-87-88-89-90-91-92-93-94-95-96-97-98-99'<\/pre>\n<p>####<\/p>\n<pre class=\"lang:default decode:true  \">'''\ntimeit.timeit(stmt='pass', setup='pass', timer=&lt;default timer&gt;, number=1000000)\n    Create a Timer instance with the given statement, setup code and timer function and run its timeit() method with number executions.\ntimeit.repeat(stmt='pass', setup='pass', timer=&lt;default timer&gt;, repeat=3, number=1000000)\n    Create a Timer instance with the given statement, setup code and timer function and run its repeat() method with the given repeat count and number executions.\n\nstmt and setup may also contain multiple statements separated by ; or newlines, as long as they don\u2019t contain multi-line string literals.\nstmt    #\u6307\u7684\u662f\u8981\u6267\u884c\u7684\u8bed\u53e5\nsetup   #\u7528\u4e8e\u524d\u671f\u51c6\u5907(\u4f8b\u5982\u5bfc\u5165\u4e00\u4e9b\u6a21\u5757\uff0c\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u7f16\u5199)\n\n$ python -m timeit [-n N] [-r N] [-s S] [-t] [-c] [-h] [statement ...]\nWhere the following options are understood:\n\n-n N, --number=N\nhow many times to execute \u2018statement\u2019\n\n-r N, --repeat=N\nhow many times to repeat the timer (default 3)\n\n-s S, --setup=S\nstatement to be executed once initially (default pass)\n\n-t, --time\nuse time.time() (default on all platforms but Windows)\n\n-c, --clock\nuse time.clock() (default on Windows)\n'''<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u641c\u7d22\u5173\u952e\u5b57\uff1a http:\/\/search.aol.com\/aol\/search?q=python+runnin [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[472,362,8,372],"class_list":["post-1978","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-cprofile","tag-ipython","tag-python","tag-timeit"],"views":3128,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=1978"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1978\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}