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)
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().
EDIT:
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)
So it looks like this:
ncalls tottime percall cumtime percall filename:lineno(function)
1 3.295 3.295 10.090 10.090 pystone.py:79(Proc0)
(So not for it to look like when I open stones.prof)
You can use the library: pstats_print2list https://pypi.python.org/pypi/pstats_print2list
pip install pstats_print2list
And usage:How about output redirection?
Stats takes an optional 'stream' argument. Simply open a file and pass the open file object to the Stats constructor as shown below. From that point any call to print_stats() will output to the stream you passed into the constructor. Hope this helps. :)
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.