Python 性能测试
先看示例代码timing_function.py:
from guppy import hpy import time import random from functools import wraps def fn_timer(function): @wraps(function) def function_timer(*args, **kwargs): t0 = time.time() result = function(*args, **kwargs) t1 = time.time() print ("Total time running %s: %s seconds" % (function.func_name, str(t1 - t0))) return result return function_timer @fn_timer def random_sort(n): return sorted([random.random() for i in range(n)]) #@profile def random_sort2(n): l = [random.random() for i in range(n)] l.sort() return l def random_sort3(n): hp = hpy() print "Heap at the beginning of the function\n", hp.heap() l = [random.random() for i in range(n)] l.sort() print "Heap at the end of the function\n", hp.heap() return l if __name__ == "__main__": #random_sort(2000000)
1.运行时间
装饰器(random_sort)
timeit(random_sort):
python -m timeit -n 4 -r 5 -s "import timing_functions" "timing_functions.random_sort(2000000)"
注:-n 4 运行测试4次; -r 5每个测试重复平均5次
系统time:
time -p python timing_functions.py 
cProfile(每个函数和方法):
python -m cProfile -s cumulative timing_functions.py
2.CPU时间
line_profiler(random_sort2):
pip install line_profiler
kernprof -l -v timing_functions.py
注:-l 逐行; -v 详细输出
3.内存使用率
memory_profiler(random_sort2):
pip install memory_profiler psutil pstil用于更快运行
python -m memory_profiler timing_functions.py
4.Heap信息
代码各阶段被创建对象(random_sort3):
pip install guppy
python timing_functions.py