Python脚本的性能分析


搜索关键字:

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/

参考链接:

==

如何检测Python脚本可能存在的内存泄漏问题?
搜索关键字:

http://search.aol.com/aol/search?q=python+memory+leakage

参考链接:

==

在Python中测试某段代码的执行时间
搜索关键字:
参考链接:
测试例子:
参考结论:

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 条评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注