Python统计信息:如何将其写入(人类可读)文件 [英] Python stats: how do I write it to a (human readable) file
问题描述
我正在使用Python的热点分析器: http://docs.python.org/2 /library/hotshot.html
I am using Python's hotshot profiler: http://docs.python.org/2/library/hotshot.html
它显示了如何打印统计信息:
It shows how to print the stats:
stats.print_stats(20)
但是如何将其保存到文件中?我不确定如何获取信息,因此可以使用write()将其写入文件.
But how do I get that into a file? I'm not sure how to get at the information so I can write it to a file using write().
我想要的结果与通过这种方式打印时得到的结果一样易于阅读:
I'd like the same easily readable result as is printed when it's done this way:
stats = hotshot.stats.load("stones.prof")
stats.strip_dirs()
stats.sort_stats('time', 'calls')
stats.print_stats(20)
所以看起来像这样:
ncalls tottime percall cumtime percall filename:lineno(function)
1 3.295 3.295 10.090 10.090 pystone.py:79(Proc0)
(因此,当我打开stones.prof时,它看起来不会像这样)
(So not for it to look like when I open stones.prof)
推荐答案
我最终重写了print_stats()函数,首先是从pstats.py复制它.它返回一个字符串,然后可以将其写入文件.我没有测试每个if-else循环,只是它可以在我需要的示例中工作.我把原来的台词注释掉了.尽管该函数不再使用"self",但我仍保留了相同的变量名.
I ended up rewriting the print_stats() function, starting with copying it from pstats.py. It returns a string, which can then be written to a file. I have not tested every if-else loop, just that it works in the examples that I needed it for. I left the original lines commented out. I've left the variable names the same although it isn't really "self" that the function is using anymore.
stats = hotshot.stats.load("stones.prof")
stats.strip_dirs()
stats.sort_stats('time', 'calls')
readable_str = xprint_stats(stats, 20)
import pstats
def xprint_stats(self, *amount):
x = ""
for filename in self.files:
x += " " + filename
#if self.files: print >> self.stream
# ?
indent = ' ' * 8
for func in self.top_level:
#print >> self.stream, indent, xfunc_get_function_name(func)
x += indent + pstats.func_get_function_name(func)
#print >> self.stream, indent, self.total_calls, "function calls",
x += indent + str(self.total_calls) + " function calls" + " "
if self.total_calls != self.prim_calls:
#print >> self.stream, "(%d primitive calls)" % self.prim_calls,
x += "(%d primitive calls)" % self.prim_calls + " "
#print >> self.stream, "in %.3f seconds" % self.total_tt
#print >> self.stream
x += "in %.3f seconds" % self.total_tt + "\n"
#width, list = stats.get_print_list(amount)
msg, width, list = xget_print_list(stats, amount)
x += msg
if list:
#self.print_title()
x += "\n" + ' ncalls tottime percall cumtime percall filename:lineno(function)'
x += "\n"
for func in list:
#self.print_line(func)
x += xprint_line(self, func) + "\n"
# print >> self.stream
# print >> self.stream
#return self
return x
def xprint_line(self, func):
x = ""
cc, nc, tt, ct, callers = self.stats[func]
c = str(nc)
if nc != cc:
c = c + '/' + str(cc)
# print >> self.stream, c.rjust(9),
# print >> self.stream, f8(tt),
x += c.rjust(9) + " "
x += pstats.f8(tt) + " "
if nc == 0:
#print >> self.stream, ' '*8,
x += ' '*8
else:
#print >> self.stream, f8(float(tt)/nc),
x += pstats.f8(float(tt)/nc) + " "
#print >> self.stream, f8(ct),
x += pstats.f8(ct) + " "
if cc == 0:
#print >> self.stream, ' '*8,
x += ' '*8
else:
#print >> self.stream, f8(float(ct)/cc),
x += pstats.f8(float(ct)/cc) + " "
#print >> self.stream, func_std_string(func)
x += pstats.func_std_string(func) + " "
return x
def xget_print_list(self, sel_list):
width = self.max_name_len
if self.fcn_list:
stat_list = self.fcn_list[:]
msg = " Ordered by: " + self.sort_type + '\n'
else:
stat_list = self.stats.keys()
msg = " Random listing order was used\n"
for selection in sel_list:
stat_list, msg = self.eval_print_amount(selection, stat_list, msg)
count = len(stat_list)
if not stat_list:
return 0, stat_list
#print >> self.stream, msg
if count < len(self.stats):
width = 0
for func in stat_list:
if len(pstats.func_std_string(func)) > width:
width = len(pstats.func_std_string(func))
#return width+2, stat_list
return msg, width+2, stat_list
这篇关于Python统计信息:如何将其写入(人类可读)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!