ASE第一次作业中,用到了peformance analysis,在python中可以用自带module——cProfile来做,记录下来流程方便以后使用。
import cProfile
import pstats
cProfile.run('CountWords("gone_with_the_wind.txt", 10, "stopwords.txt","verbs.txt")', 'restats')
p = pstats.Stats('restats')
p.strip_dirs().sort_stats('time').print_stats()
1、对待小项目,只需要简单输出函数耗时即可,将上面代码嵌入要测试的脚本,即可输出如下的分析内容,详细解释可以参考cProfile.
ncalls tottime percall cumtime percall filename:lineno(function)
2 0.272 0.136 0.272 0.136 {method 'readlines' of '_io._IOBase' objects}
1 0.251 0.251 0.251 0.251 {method 'findall' of '_sre.SRE_Pattern' objects}
1 0.203 0.203 0.203 0.203 {method 'read' of '_