搜索关键字:
http://search.aol.com/aol/search?q=python+running+performance+analysis
参考结论:
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.
You can call it from within your code, or from the interpreter, like this:
import cProfile cProfile.run('foo()')
Even more usefully, you can invoke the cProfile when running a script:
$ python -m cProfile myscript.py
To make it even easier, I made a little batch file called ‘profile.bat’:
python -m cProfile %1
So all I have to do is run:
$ profile euler048.py
And I get this:
1007 function calls in 0.061 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.061 0.061 <string>:1(<module>) 1000 0.051 0.000 0.051 0.000 euler048.py:2(<lambda>) 1 0.005 0.005 0.061 0.061 euler048.py:2(<module>) 1 0.000 0.000 0.061 0.061 {execfile} 1 0.002 0.002 0.053 0.053 {map} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler objects} 1 0.000 0.000 0.000 0.000 {range} 1 0.003 0.003 0.003 0.003 {sum}
EDIT: Updated link to a good video resource from PyCon 2013: http://lanyrd.com/2013/pycon/scdywg/
参考链接:
- http://stackoverflow.com/questions/582336/how-can-you-profile-a-python-script #简单直接!
- https://docs.python.org/2/library/profile.html
- http://stackoverflow.com/questions/7069733/how-do-i-read-the-output-of-the-ipython-prun-profiler-command
- http://ymichael.com/2014/03/08/profiling-python-with-cprofile.html
- http://pymotw.com/2/profile
- https://zapier.com/engineering/profiling-python-boss/
- https://zapier.com/engineering/debugging-python-boss/ #调试Python(但各人更倾向于print)
- http://www.huyng.com/posts/python-performance-analysis/
- http://stackoverflow.com/questions/110259/which-python-memory-profiler-is-recommended
==
如何检测Python脚本可能存在的内存泄漏问题?
搜索关键字:
http://search.aol.com/aol/search?q=python+memory+leakage
参考链接:
- http://stackoverflow.com/questions/110259/which-python-memory-profiler-is-recommended
- http://stackoverflow.com/questions/1641231/python-working-around-memory-leaks/
- http://www.lshift.net/blog/2008/11/14/tracing-python-memory-leaks/
- http://mg.pov.lt/blog/hunting-python-memleaks.html
- http://chase-seibert.github.io/blog/2013/08/03/diagnosing-memory-leaks-python.html
- http://stackoverflow.com/questions/1435415/python-memory-leaks
- https://pythonhosted.org/Pympler/tutorials/muppy_tutorial.html
- https://pythonhosted.org/Pympler/muppy.html
==
在Python中测试某段代码的执行时间
搜索关键字:
参考链接:
- http://stackoverflow.com/questions/7370801/measure-time-elapsed-in-python
- http://stackoverflow.com/questions/1685221/accurately-measure-time-python-function-takes
- https://docs.python.org/2/library/profile.html
- http://stackoverflow.com/questions/85451/python-time-clock-vs-time-time-accuracy
- http://stackoverflow.com/questions/1557571/how-to-get-time-of-a-python-program-execution
- http://stackoverflow.com/questions/15707056/get-time-of-execution-of-a-block-of-code-in-python-2-7
- http://stackoverflow.com/questions/8220801/how-to-use-timeit-module
- http://pymotw.com/2/timeit/
- https://docs.python.org/2.7/library/debug.html
- https://docs.python.org/2.7/library/timeit.html
测试例子:
- http://stackoverflow.com/questions/4901523/whats-a-faster-operation-re-match-search-or-str-find
- http://stackoverflow.com/questions/4172131/create-random-list-of-integers-in-python
- http://stackoverflow.com/questions/5086430/how-to-pass-parameters-of-a-function-when-using-timeit-timer
参考结论:
Given test.py:
... def InsertionSort(): ... def TimSort(): ...
run timeit like this:
% python -mtimeit -s'import test' 'test.InsertionSort()' % python -mtimeit -s'import test' 'test.TimSort()'
The following example shows how the Command-Line Interface can be used to compare three different expressions:
$ python -m timeit '"-".join(str(n) for n in range(100))' 10000 loops, best of 3: 40.3 usec per loop $ python -m timeit '"-".join([str(n) for n in range(100)])' 10000 loops, best of 3: 33.4 usec per loop $ python -m timeit '"-".join(map(str, range(100)))' 10000 loops, best of 3: 25.2 usec per loop
This can be achieved from the Python Interface with:
>>> import timeit >>> timeit.timeit('"-".join(str(n) for n in range(100))', number=10000) 0.8187260627746582 >>> timeit.timeit('"-".join([str(n) for n in range(100)])', number=10000) 0.7288308143615723 >>> timeit.timeit('"-".join(map(str, range(100)))', number=10000) 0.5858950614929199
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.
https://docs.python.org/2/library/timeit.html
#!/usr/bin/env python # coding=utf-8 import time def elapse_time(func): def wrapper(*args, **kwargs): beg_ts = time.time() func(*args, **kwargs) end_ts = time.time() print("elapsed time: %f" % (end_ts - beg_ts)) return wrapper @elapse_time def test(): for i in xrange(0, 10000): pass if __name__ == "__main__": test() #### def test(): """Stupid test function""" L = [] for i in range(100): L.append(i) if __name__ == '__main__': import timeit print(timeit.timeit("test()", setup="from __main__ import test"))
####
$ python -m timeit -s 'text = "sample string"; char = "g"' 'char in text' 10000000 loops, best of 3: 0.0877 usec per loop $ python -m timeit -s 'text = "sample string"; char = "g"' 'text.find(char)' 1000000 loops, best of 3: 0.342 usec per loop # >>> import timeit >>> t = timeit.Timer('char in text', setup='text = "sample string"; char = "g"') >>> t.timeit() 0.3955516149999312 >>> t.repeat() [0.40193588800002544, 0.3960157959998014, 0.39594301399984033] # In [2]: %timeit? Usage, in line mode: %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement or in cell mode: %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code code code... # #==不建议使用IPython的 %time 魔术方法,因为它会打印出结果,除非你真的需要的话== In [10]: %time? Docstring: Time execution of a Python statement or expression. Examples: In [1]: %time 2**128 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s Wall time: 0.00 Out[1]: 340282366920938463463374607431768211456L In [9]: %time "-".join(str(n) for n in range(100)) Wall time: 0 ns Out[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'
####
''' timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000) Create a Timer instance with the given statement, setup code and timer function and run its timeit() method with number executions. timeit.repeat(stmt='pass', setup='pass', timer=<default timer>, repeat=3, number=1000000) 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. stmt and setup may also contain multiple statements separated by ; or newlines, as long as they don’t contain multi-line string literals. stmt #指的是要执行的语句 setup #用于前期准备(例如导入一些模块,自定义函数的编写) $ python -m timeit [-n N] [-r N] [-s S] [-t] [-c] [-h] [statement ...] Where the following options are understood: -n N, --number=N how many times to execute ‘statement’ -r N, --repeat=N how many times to repeat the timer (default 3) -s S, --setup=S statement to be executed once initially (default pass) -t, --time use time.time() (default on all platforms but Windows) -c, --clock use time.clock() (default on Windows) '''
《“Python脚本的性能分析”》 有 1 条评论
这款神器,能把 Python 代码执行过程看地一清二楚
https://mp.weixin.qq.com/s/F1M-jPR7w55-ShNrMwI0KQ
https://github.com/alexmojaki/heartrate