{"id":1471,"date":"2014-10-12T07:45:29","date_gmt":"2014-10-12T07:45:29","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1471"},"modified":"2014-10-12T07:45:29","modified_gmt":"2014-10-12T07:45:29","slug":"ipython%e4%bd%bf%e7%94%a8tips_2","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1471.html","title":{"rendered":"IPython\u4f7f\u7528tips_2"},"content":{"rendered":"<h5>1.\u6d4b\u8bd5\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\uff1a%time\u548c%timeit<\/h5>\n<p>\u5bf9\u4e8e\u89c4\u6a21\u66f4\u5927\u3001\u8fd0\u884c\u65f6\u95f4\u66f4\u957f\u7684\u6570\u636e\u5206\u6790\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f60\u53ef\u80fd\u4f1a\u5e0c\u671b\u6d4b\u8bd5\u4e00\u4e0b\u5404\u4e2a\u90e8\u5206\u6216\u51fd\u6570\u8c03\u7528\u6216\u8bed\u53e5\u7684\u6267\u884c\u65f6\u95f4\u3002\u4f60\u53ef\u80fd\u4f1a\u5e0c\u671b\u4e86\u89e3\u67d0\u4e2a\u590d\u6742\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\u5230\u5e95\u662f\u54ea\u4e9b\u51fd\u6570\u5360\u7528\u7684\u65f6\u95f4\u6700\u591a\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u5728\u5f00\u53d1\u548c\u6d4b\u8bd5\u4ee3\u7801\u7684\u8fc7\u7a0b\u4e2d\uff0cIPython\u80fd\u591f\u8ba9\u4f60\u8f7b\u677e\u5f97\u5230\u8fd9\u4e9b\u4fe1\u606f\u3002\u4f7f\u7528\u5185\u7f6e\u7684time\u6a21\u5757\u53ca\u5176time.clock\u548ctime.time\u51fd\u6570\u624b\u5de5\u6d4b\u8bd5\u4ee3\u7801\u6267\u884c\u65f6\u95f4\u662f\u4e00\u4ef6\u4ee4\u4eba\u70e6\u95f7\u7684\u4e8b\u60c5\uff0c\u56e0\u4e3a\u4f60\u5fc5\u987b\u7f16\u5199\u8bb8\u591a\u4e00\u6a21\u4e00\u6837\u7684\u4e86\u65e0\u751f\u8da3\u7684\u516c\u5f0f\u5316\u4ee3\u7801\uff1a<\/p>\n<pre class=\"lang:default decode:true\">import time\nstart = time.time()\nfor i in range(iterations):\n    # \u8fd9\u91cc\u653e\u4e00\u4e9b\u5f85\u6267\u884c\u7684\u4ee3\u7801\nelapsed_per = (time.time() - start) \/ iterations<\/pre>\n<p>\u7531\u4e8e\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u7528\u7684\u529f\u80fd\uff0c\u6240\u4ee5IPython\u4e13\u95e8\u63d0\u4f9b\u4e86\u4e24\u4e2a\u9b54\u672f\u51fd\u6570\uff08%time\u548c%timeit\uff09\u4ee5\u4fbf\u81ea\u52a8\u5b8c\u6210\u8be5\u8fc7\u7a0b\u3002<span style=\"color: #ff0000;\"><strong>%time\u4e00\u6b21\u6267\u884c\u4e00\u6761\u8bed\u53e5\uff0c\u7136\u540e\u62a5\u544a\u603b\u4f53\u6267\u884c\u65f6\u95f4\u3002<\/strong><\/span>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u5927\u5806\u5b57\u7b26\u4e32\uff0c\u5e0c\u671b\u5bf9\u51e0\u4e2a\u201c\u80fd\u591f\u9009\u51fa\u5177\u6709\u7279\u6b8a\u524d\u7f00\u7684\u5b57\u7b26\u4e32\u201d\u7684\u51fd\u6570\u8fdb\u884c\u6bd4\u8f83\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u62e5\u670960\u4e07\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\uff0c\u4ee5\u53ca\u4e24\u4e2a\u4e0d\u540c\u7684\u201c\u80fd\u591f\u9009\u51fa\u5176\u4e2d\u4ee5foo\u5f00\u5934\u7684\u5b57\u7b26\u4e32\u201d\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"lang:default decode:true\"># \u4e00\u4e2a\u975e\u5e38\u5927\u7684\u5b57\u7b26\u4e32\u6570\u7ec4\nstrings = ['foo', 'foobar', 'baz', 'qux', 'python', 'Guido Van Rossum'] * 100000\n\nmethod1 = [x for x in strings if x.startswith('foo')]\n\nmethod2 = [x for x in strings if x[:3] == 'foo']<\/pre>\n<p>\u770b\u4e0a\u53bb\u5b83\u4eec\u7684\u6027\u80fd\u8868\u73b0\u5e94\u8be5\u5dee\u4e0d\u591a\uff0c\u5bf9\u5427\uff1f\u6211\u4eec\u901a\u8fc7%time\u6765\u786e\u8ba4\u4e00\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true\">In [561]: %time method1 = [x for x in strings if x.startswith('foo')]\nCPU times: user 0.19 s, sys: 0.00 s, total: 0.19 s\nWall time: 0.19 s\n\nIn [562]: %time method2 = [x for x in strings if x[:3] == 'foo']\nCPU times: user 0.09 s, sys: 0.00 s, total: 0.09 s\nWall time: 0.09 s<\/pre>\n<p>\u5899\u4e0a\u65f6\u95f4\uff08Wall time\uff09\u662f\u6211\u4eec\u6700\u611f\u5174\u8da3\u7684\u6570\u5b57\u3002\u6240\u4ee5\uff0c\u770b\u4e0a\u53bb\u7b2c\u4e00\u4e2a\u65b9\u6cd5\u8017\u8d39\u4e86\u4e24\u500d\u4ee5\u4e0a\u7684\u65f6\u95f4\uff0c\u4f46\u8fd9\u5e76\u4e0d\u662f\u4e00\u4e2a\u975e\u5e38\u7cbe\u786e\u7684\u7ed3\u679c\u3002\u5982\u679c\u4f60\u5bf9\u76f8\u540c\u8bed\u53e5\u591a\u6b21\u6267\u884c%time\u7684\u8bdd\uff0c\u5c31\u4f1a\u53d1\u73b0\u5176\u7ed3\u679c\u662f\u4f1a\u53d8\u7684\u3002<strong><span style=\"color: #ff0000;\">\u4e3a\u4e86\u5f97\u5230\u66f4\u4e3a\u7cbe\u786e\u7684\u7ed3\u679c\uff0c\u9700\u8981\u4f7f\u7528\u9b54\u672f\u51fd\u6570%timeit\u3002\u5bf9\u4e8e\u4efb\u610f\u8bed\u53e5\uff0c\u5b83\u4f1a\u81ea\u52a8\u591a\u6b21\u6267\u884c\u4ee5\u4ea7\u751f\u4e00\u4e2a\u975e\u5e38\u7cbe\u786e\u7684\u5e73\u5747\u6267\u884c\u65f6\u95f4\u3002<\/span><\/strong><\/p>\n<pre class=\"lang:default decode:true\">In [563]: %timeit [x for x in strings if x.startswith('foo')]\n10 loops, best of 3: 159 ms per loop\n\u3000\u3000\nIn [564]: %timeit [x for x in strings if x[:3] == 'foo']\n10 loops, best of 3: 59.3 ms per loop<\/pre>\n<p>\u8fd9\u4e2a\u8c8c\u4f3c\u5e73\u6de1\u65e0\u5947\u7684\u4f8b\u5b50\u6b63\u597d\u8bf4\u660e\u4e86\u4e00\u4e2a\u4e8b\u5b9e\uff1a<span style=\"color: #ff0000;\"><strong>\u6211\u4eec\u975e\u5e38\u6709\u5fc5\u8981\u4e86\u89e3Python\u6807\u51c6\u5e93\u3001NumPy\u3001pandas\u4ee5\u53ca\u672c\u4e66\u4e2d\u6240\u7528\u5230\u7684\u5176\u4ed6\u5e93\u7684\u6027\u80fd\u7279\u70b9<\/strong><\/span>\u3002\u5728\u5927\u578b\u6570\u636e\u5206\u6790\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u8fd9\u4e9b\u4e0d\u8d77\u773c\u7684\u6beb\u79d2\u6570\u662f\u4f1a\u4e0d\u65ad\u7d2f\u79ef\u7684\uff01<\/p>\n<p><span style=\"color: #ff0000;\">\u5bf9\u4e8e\u90a3\u4e9b\u6267\u884c\u65f6\u95f4\u975e\u5e38\u77ed\uff08\u751a\u81f3\u662f\u90a3\u4e9b\u5fae\u79d2\uff081e-6\u79d2\uff09\u6216\u7eb3\u79d2\uff081e-9\u79d2\uff09\u7ea7\u7684\uff09\u7684\u5206\u6790\u8bed\u53e5\u548c\u51fd\u6570\u800c\u8a00\uff0c%timeit\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u867d\u7136\u8fd9\u4e9b\u65f6\u95f4\u503c\u5c0f\u5230\u51e0\u4e4e\u53ef\u4ee5\u5ffd\u7565\u4e0d\u8ba1\uff0c\u4f46\u540c\u6837\u6267\u884c100\u4e07\u6b21\u4e00\u4e2a20\u5fae\u79d2\u7684\u51fd\u6570\uff0c\u6240\u7528\u7684\u65f6\u95f4\u8981\u6bd4\u4e00\u4e2a5\u5fae\u79d2\u7684\u591a15\u79d2\u3002<\/span>\u5728\u4e0a\u9762\u90a3\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5bf9\u90a3\u4e24\u4e2a\u5b57\u7b26\u4e32\u8fd0\u7b97\u8fdb\u884c\u6bd4\u8f83\u4ee5\u4e86\u89e3\u5176\u6027\u80fd\u7279\u70b9\uff1a<\/p>\n<pre class=\"lang:default decode:true\">In [565]: x = 'foobar'\n\nIn [566]: y = 'foo'\n\nIn [567]: %timeit x.startswith(y)\n1000000 loops, best of 3: 267 ns per loop\n\nIn [568]: %timeit x[:3] == y\n10000000 loops, best of 3: 147 ns per loop<\/pre>\n<h5>2.\u57fa\u672c\u6027\u80fd\u5206\u6790\uff1a%prun\u548c%run -p<\/h5>\n<p><strong><span style=\"color: #ff0000;\">\u4ee3\u7801\u7684\u6027\u80fd\u5206\u6790\u8ddf\u4ee3\u7801\u6267\u884c\u65f6\u95f4\u5bc6\u5207\u76f8\u5173\uff0c\u53ea\u4e0d\u8fc7\u5b83\u5173\u6ce8\u7684\u662f\u8017\u8d39\u65f6\u95f4\u7684\u4f4d\u7f6e\u3002<\/span><\/strong>\u4e3b\u8981\u7684Python\u6027\u80fd\u5206\u6790\u5de5\u5177\u662fcProfile\u6a21\u5757\uff0c\u5b83\u4e0d\u662f\u4e13\u4e3aIPython\u8bbe\u8ba1\u7684\u3002cProfile\u5728\u6267\u884c\u4e00\u4e2a\u7a0b\u5e8f\u6216\u4ee3\u7801\u5757\u65f6\uff0c\u4f1a\u8bb0\u5f55\u5404\u51fd\u6570\u6240\u8017\u8d39\u7684\u65f6\u95f4\u3002<\/p>\n<p>cProfile\u4e00\u822c\u662f\u5728\u547d\u4ee4\u884c\u4e0a\u4f7f\u7528\u7684\uff0c\u5b83\u5c06\u6267\u884c\u6574\u4e2a\u7a0b\u5e8f\u7136\u540e\u8f93\u51fa\u5404\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u811a\u672c\uff1a\u5728\u4e00\u4e2a\u5faa\u73af\u4e2d\u6267\u884c\u4e00\u4e9b\u7ebf\u6027\u4ee3\u6570\u8ba1\u7b97\uff08\u8ba1\u7b97\u4e00\u4e2a100\u00d7100\u7684\u77e9\u9635\u7684\u6700\u5927\u672c\u5f81\u503c\u7edd\u5bf9\u503c\uff09\u3002<\/p>\n<pre class=\"lang:default decode:true\">import numpy as np\nfrom numpy.linalg import eigvals\n\ndef run_experiment(niter=100):\n    K = 100\n    results = []\n    for _ in xrange(niter):\n        mat = np.random.randn(K, K)\n        max_eigenvalue = np.abs(eigvals(mat)).max()\n        results.append(max_eigenvalue)\n    return results\nsome_results = run_experiment()\nprint 'Largest one we saw: %s' % np.max(some_results)<\/pre>\n<p>\u5982\u679c\u4f60\u8fd8\u4e0d\u61c2NumPy\uff0c\u6682\u65f6\u5148\u522b\u7ba1\uff0c\u540e\u9762\u4f1a\u8bb2\u7684\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4e0b\u5217\u547d\u4ee4\u5373\u53ef\u901a\u8fc7cProfile\u542f\u52a8\u8be5\u811a\u672c\uff1a<br \/>\npython -m cProfile cprof_example.py<\/p>\n<p>\u6267\u884c\u4e4b\u540e\uff0c\u4f60\u4f1a\u53d1\u73b0\u8f93\u51fa\u7ed3\u679c\u662f\u6309\u51fd\u6570\u540d\u6392\u5e8f\u7684\u3002\u8fd9\u8ba9\u6211\u4eec\u5f88\u96be\u53d1\u73b0\u54ea\u91cc\u624d\u662f\u6700\u82b1\u65f6\u95f4\u7684\u5730\u65b9\uff0c\u56e0\u6b64\u901a\u5e38\u90fd\u4f1a\u518d\u7528-s\u6807\u8bb0\u6307\u5b9a\u4e00\u4e2a\u6392\u5e8f\u89c4\u5219\uff1a<\/p>\n<pre class=\"lang:default decode:true\">$ python -m cProfile -s cumulative cprof_example.py\nLargest one we saw: 11.923204422\n     15116 function calls (14927 primitive calls) in 0.720 seconds\n\nOrdered by: cumulative time\n\nncalls tottime percall cumtime percall filename:lineno(function)\n1 0.001 0.001 0.721 0.721 cprof_example.py:1(&lt;module&gt;)\n100 0.003 0.000 0.586 0.006 linalg.py:702(eigvals)\n200 0.572 0.003 0.572 0.003 {numpy.linalg.lapack_lite.dgeev}\n1 0.002 0.002 0.075 0.075 __init__.py:106(&lt;module&gt;)\n100 0.059 0.001 0.059 0.001 {method 'randn')\n1 0.000 0.000 0.044 0.044 add_newdocs.py:9(&lt;module&gt;)\n2 0.001 0.001 0.037 0.019 __init__.py:1(&lt;module&gt;)\n2 0.003 0.002 0.030 0.015 __init__.py:2(&lt;module&gt;)\n1 0.000 0.000 0.030 0.030 type_check.py:3(&lt;module&gt;)\n1 0.001 0.001 0.021 0.021 __init__.py:15(&lt;module&gt;)\n1 0.013 0.013 0.013 0.013 numeric.py:1(&lt;module&gt;)\n1 0.000 0.000 0.009 0.009 __init__.py:6(&lt;module&gt;)\n1 0.001 0.001 0.008 0.008 __init__.py:45(&lt;module&gt;)\n262 0.005 0.000 0.007 0.000 function_base.py:3178(add_newdoc)\n100 0.003 0.000 0.005 0.000 linalg.py:162(_assertFinite)\n...<\/pre>\n<p>\u8fd9\u91cc\u53ea\u7ed9\u51fa\u4e86\u8f93\u51fa\u7ed3\u679c\u4e2d\u7684\u524d15\u884c\u3002\u53ea\u9700\u67e5\u770bcumtime\u5217\u5373\u53ef\u53d1\u73b0\u5404\u51fd\u6570\u6240\u8017\u8d39\u7684\u603b\u65f6\u95f4\u3002\u6ce8\u610f\uff0c\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u4e86\u522b\u7684\u51fd\u6570\uff0c\u8ba1\u65f6\u5668\u662f\u4e0d\u4f1a\u505c\u4e0b\u6765\u91cd\u65b0\u8ba1\u65f6\u7684\u3002cProfile\u8bb0\u5f55\u7684\u662f\u5404\u51fd\u6570\u8c03\u7528\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u65f6\u95f4\uff0c\u5e76\u4f9d\u6b64\u8ba1\u7b97\u603b\u65f6\u95f4\u3002<\/p>\n<p><span style=\"color: #ff0000;\">\u9664\u547d\u4ee4\u884c\u7528\u6cd5\u4e4b\u5916\uff0ccProfile\u8fd8\u53ef\u4ee5\u7f16\u7a0b\u7684\u65b9\u5f0f\u5206\u6790\u4efb\u610f\u4ee3\u7801\u5757\u7684\u6027\u80fd\u3002IPython\u4e3a\u6b64\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b9\u4fbf\u7684\u63a5\u53e3\uff0c\u5373%prun\u547d\u4ee4\u548c\u5e26-p\u9009\u9879\u7684%run\u3002<\/span>%prun\u7684\u683c\u5f0f\u8ddfcProfile\u5dee\u4e0d\u591a\uff0c\u4f46\u5b83\u5206\u6790\u7684\u662fPython\u8bed\u53e5\u800c\u4e0d\u662f\u6574\u4e2a.py\u6587\u4ef6\uff1a<\/p>\n<pre class=\"lang:default decode:true\">In [4]: %prun -l 7 -s cumulative run_experiment()\n         4203 function calls in 0.643 seconds\n\nOrdered by: cumulative time\nList reduced from 32 to 7 due to restriction &lt;7&gt;\n\nncalls  tottime  percall  cumtime  percall filename:lineno(function)\n     1   0.000    0.000    0.643    0.643 &lt;string&gt;:1(&lt;module&gt;)\n     1   0.001    0.001    0.643    0.643 cprof_example.py:4(run_experiment)\n   100   0.003    0.000    0.583    0.006 linalg.py:702(eigvals)\n   200   0.569    0.003    0.569    0.003 {numpy.linalg.lapack_lite.dgeev}\n   100    0.058    0.001    0.058    0.001 {method 'randn'}\n   100   0.003    0.000    0.005    0.000 linalg.py:162(_assertFinite)\n   200    0.002    0.000    0.002    0.000 {method 'all' of 'numpy.ndarray' objects}<\/pre>\n<p>\u6267\u884c%run -p -s cumulative cprof_example.py\u4e5f\u80fd\u8fbe\u5230\u4e0a\u9762\u90a3\u6761\u7cfb\u7edf\u547d\u4ee4\u884c\u547d\u4ee4\u4e00\u6837\u7684\u6548\u679c\uff0c\u4f46\u662f\u5374\u65e0\u9700\u9000\u51faIPython\u3002<\/p>\n<h5>3.\u9010\u884c\u5206\u6790\u51fd\u6570\u6027\u80fd<\/h5>\n<p>\u6709\u4e9b\u65f6\u5019\uff0c\u4ece%prun\uff08\u6216\u5176\u4ed6\u57fa\u4e8ecProfile\u7684\u6027\u80fd\u5206\u6790\u624b\u6bb5\uff09\u5f97\u5230\u7684\u4fe1\u606f\u8981\u4e48\u4e0d\u8db3\u4ee5\u8bf4\u660e\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\uff0c\u8981\u4e48\u5c31\u590d\u6742\u5230\u96be\u4ee5\u7406\u89e3\uff08\u6309\u51fd\u6570\u540d\u805a\u5408\uff09\u3002\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u53eb\u505aline_profiler\u7684\u5c0f\u578b\u5e93\uff08\u53ef\u4ee5\u901a\u8fc7PyPI\u6216\u968f\u4fbf\u4e00\u79cd\u5305\u7ba1\u7406\u5de5\u5177\u83b7\u53d6\uff09\u3002<strong><span style=\"color: #ff0000;\">\u5176\u4e2d\u6709\u4e00\u4e2a\u65b0\u7684\u9b54\u672f\u51fd\u6570%lprun\uff0c\u5b83\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u6216\u591a\u4e2a\u51fd\u6570\u8fdb\u884c\u9010\u884c\u6027\u80fd\u5206\u6790\u3002\u4f60\u53ef\u4ee5\u4fee\u6539IPython\u914d\u7f6e\uff08\u53c2\u8003IPython\u6587\u4ef6\u6216\u672c\u7ae0\u7a0d\u540e\u5173\u4e8e\u914d\u7f6e\u7684\u5185\u5bb9\uff09\u4ee5\u542f\u7528\u8fd9\u4e2a\u6269\u5c55<\/span><\/strong>\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"lang:default decode:true\"># A list of dotted module names of IPython extensions to load.\nc.TerminalIPythonApp.extensions = ['line_profiler']<\/pre>\n<p>line_profiler\u53ef\u4ee5\u901a\u8fc7\u7f16\u7a0b\u7684\u65b9\u5f0f\u4f7f\u7528\uff08\u8bf7\u53c2\u9605\u5b8c\u6574\u6587\u6863\uff09\uff0c\u4f46\u5176\u6700\u5f3a\u5927\u7684\u4e00\u9762\u5374\u662f\u5728IPython\u4e2d\u7684\u4ea4\u4e92\u5f0f\u4f7f\u7528\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2aprof_mod\u6a21\u5757\uff0c\u5176\u4e2d\u6709\u4e00\u4e9b\u7528\u4e8eNumPy\u6570\u7ec4\u8ba1\u7b97\u7684\u4ee3\u7801\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"lang:default decode:true\">from numpy.random import randn\n\ndef add_and_sum(x, y):\n    added = x + y\n    summed = added.sum(axis=1)\n    return summed\n\ndef call_function():\n    x = randn(1000, 1000)\n    y = randn(1000, 1000)\n    return add_and_sum(x, y)<\/pre>\n<p>\u5982\u679c\u6211\u4eec\u60f3\u4e86\u89e3add_and_sum\u51fd\u6570\u7684\u6027\u80fd\uff0c%prun\u4f1a\u7ed9\u51fa\u5982\u4e0b\u6240\u793a\u7684\u7ed3\u679c\uff1a<\/p>\n<pre class=\"lang:default decode:true\">In [569]: %run prof_mod\n\nIn [570]: x = randn(3000, 3000)\nIn [571]: y = randn(3000, 3000)\nIn [572]: %prun add_and_sum(x, y)\n        4 function calls in 0.049 seconds\nOrdered by: internal time\nncalls  tottime  percall  cumtime  percall filename:lineno(function)\n     1  0.036   0.036  0.046 0.046 prof_mod.py:3(add_and_sum)\n     1  0.009   0.009  0.009 0.009 {method 'sum' of 'numpy.ndarray' objects}\n     1  0.003   0.003  0.049 0.049 &lt;string&gt;:1(&lt;module&gt;)\n     1  0.000   0.000  0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}<\/pre>\n<p>\u8fd9\u4e2a\u7ed3\u679c\u5e76\u4e0d\u80fd\u8bf4\u660e\u4ec0\u4e48\u95ee\u9898\u3002\u542f\u7528line_profiler\u8fd9\u4e2aIPython\u6269\u5c55\u4e4b\u540e\uff0c\u5c31\u4f1a\u51fa\u73b0\u4e00\u4e2a\u65b0\u7684\u9b54\u672f\u547d\u4ee4%lprun\u3002\u7528\u6cd5\u4e0a\u552f\u4e00\u7684\u533a\u522b\u5c31\u662f\uff1a\u5fc5\u987b\u4e3a%lprun\u6307\u660e\u60f3\u8981\u6d4b\u8bd5\u54ea\u4e2a\u6216\u54ea\u4e9b\u51fd\u6570\u3002%lprun\u7684\u901a\u7528\u8bed\u6cd5\u4e3a\uff1a<br \/>\n%lprun -f func1 -f func2 statement_to_profile<\/p>\n<p>\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u60f3\u8981\u6d4b\u8bd5\u7684\u662fadd_and_sum\uff0c\u4e8e\u662f\u6267\u884c\uff1a<\/p>\n<pre class=\"lang:default decode:true\">In [573]: %lprun -f add_and_sum add_and_sum(x, y)\nTimer unit: 1e-06 s\nFile: book_scripts\/prof_mod.py\nFunction: add_and_sum at line 3\nTotal time: 0.045936 s\nLine #      Hits         Time  Per Hit   % Time  Line Contents\n==============================================================\n     3                                           def add_and_sum(x, y):\n     4      1           36510  36510.0     79.5       added = x + y\n     5      1            9425   9425.0     20.5       summed = added.sum(axis=1)\n     6      1               1      1.0      0.0       return summed<\/pre>\n<p>\u8fd9\u4e2a\u7ed3\u679c\u5c31\u5bb9\u6613\u7406\u89e3\u591a\u4e86\u3002\u8fd9\u91cc\u6211\u4eec\u6d4b\u8bd5\u7684\u53ea\u662fadd_and_sum\u8fd9\u4e00\u4e2a\u51fd\u6570\u3002\u4e0a\u9762\u90a3\u4e2a\u6a21\u5757\u4e2d\u8fd8\u6709\u4e00\u4e2acall_function\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408add_and_sum\u4e00\u8d77\u6d4b\u8bd5\uff0c\u4e8e\u662f\u6700\u7ec8\u7684\u6d4b\u8bd5\u547d\u4ee4\u5c31\u6210\u4e86\u4e0b\u9762\u8fd9\u4e2a\u6837\u5b50\uff1a<\/p>\n<pre class=\"lang:default decode:true\">In [574]: %lprun -f add_and_sum -f call_function call_function()\nTimer unit: 1e-06 s\nFile: book_scripts\/prof_mod.py\nFunction: add_and_sum at line 3\nTotal time: 0.005526 s\nLine # Hits Time  Per Hit % Time  Line Contents\n==============================================================\n    3                               def add_and_sum(x, y):\n    4   1 4375 4375.0 79.2    added = x + y\n    5   1 1149 1149.0 20.8    summed = added.sum(axis=1)\n    6   1    2    2.0  0.0    return summed\nFile: book_scripts\/prof_mod.py\nFunction: call_function at line 8\nTotal time: 0.121016 s\nLine # Hits Time    Per Hit % Time  Line Contents\n==============================================================\n    8     def call_function():\n    9  1 57169 57169.0  47.2     x = randn(1000, 1000)\n   10  1 58304 58304.0  48.2     y = randn(1000, 1000)\n   11  1  5543  5543.0   4.6     return add_and_sum(x, y)<\/pre>\n<p><strong><span style=\"color: #ff0000;\">\u901a\u5e38\uff0c\u6211\u4f1a\u7528%prun\uff08cProfile\uff09\u505a\u201c\u5b8f\u89c2\u7684\u201d\u6027\u80fd\u5206\u6790\uff0c\u800c\u7528%lprun\uff08line_profiler\uff09\u505a\u201c\u5fae\u89c2\u7684\u201d \u6027\u80fd\u5206\u6790\u3002<\/span><\/strong>\u8fd9\u4e24\u4e2a\u5de5\u5177\u90fd\u5f88\u6709\u5fc5\u8981\u4e86\u89e3\u4e00\u4e0b\u3002<\/p>\n<p>\u6ce8\u610f\uff1a \u5728\u4f7f\u7528%lprun\u65f6\uff0c\u4e4b\u6240\u4ee5\u5fc5\u987b\u663e\u5f0f\u6307\u660e\u5f85\u6d4b\u8bd5\u7684\u51fd\u6570\u540d\uff0c\u662f\u56e0\u4e3a\u201c\u8ddf\u8e2a\u201d\u6bcf\u4e00\u884c\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\u6240\u9700\u7684\u5f00\u9500\u5f88\u5927\u3002\u5bf9\u4e0d\u611f\u5174\u8da3\u7684\u51fd\u6570\u8fdb\u884c\u8ddf\u8e2a\u5c06\u4f1a\u5bf9\u6027\u80fd\u5206\u6790\u7ed3\u679c\u9020\u6210\u663e\u8457\u7684\u5f71\u54cd\u3002<\/p>\n<p>&nbsp;<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/book.2cto.com\/201311\/35866.html\" target=\"_blank\">\u5229\u7528Python\u8fdb\u884c\u6570\u636e\u5206\u6790<\/a><\/li>\n<li><a href=\"https:\/\/wiki.python.org\/moin\/PythonSpeed\/PerformanceTips\" target=\"_blank\">PythonSpeed\/PerformanceTips &#8211; Python Wiki<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1.\u6d4b\u8bd5\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\uff1a%time\u548c%timeit \u5bf9\u4e8e\u89c4\u6a21\u66f4\u5927\u3001\u8fd0\u884c\u65f6\u95f4\u66f4\u957f\u7684\u6570\u636e\u5206\u6790\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f60\u53ef\u80fd\u4f1a\u5e0c\u671b [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,12],"tags":[362,372],"class_list":["post-1471","post","type-post","status-publish","format-standard","hentry","category-programing","category-tools","tag-ipython","tag-timeit"],"views":2724,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1471","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=1471"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1471\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}