本文主要是介绍Python番外(3)——P72性能分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
首先将下列代码保存到cprof_example.py文件:
import numpy as np
from numpy.linalg import eigvals'''函数'''
def run_experiment(niter = 100):K = 100results = []for _ in xrange(niter):mat = np.random.randn(K,K)max_eigenvalue = np.abs(eigvals(mat)).max()results.append(max_eigenvalue)return results'''调用'''
some_results = run_experiment()
print 'Largest one we saw:%s' %np.max(some_results)
然后再命令行中,到该目录下执行:
python -m cProfile -s cumulative cprof_example.py
执行上面代码,命令行会出现很多行数据,不方便阅读,可改为:
python -m cProfile -o log.txt cprof_example.py
这样,结果会保存在log.txt中,log.txt可以用VPT(http://visualpytune.googlecode.com)打开,要翻墙,这篇博文有翻墙工具:
MLAPP
这里有更详细的cProfile的资料:
https://docs.python.org/2/library/profile.html
这篇关于Python番外(3)——P72性能分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!