Help getting profile information programmatically

Mike Driscoll kyosohma at gmail.com
Thu Dec 31 14:37:10 EST 2009


On Dec 31, 12:32 pm, sawilla <sawi... at gmail.com> wrote:
> I need to get the cumulative process time for a function that returns
> arguments and I'm having trouble doing so programmatically. After I
> get the process time, I want to log it to a file, along with other
> information. My problem is that I can't figure out how to do this
> programmatically in a good way. I see that the cProfile module has the
> method "getstats" which returns a list with the information I need but
> I don't see how I can access it.
>
> Here is some pseudocode showing what I'm hoping to do:
>
> # Get a cProfile object with the profile information of my function.
> # This syntax doesn't work and this is the first part I need help
> with.
> p = cProfile.run('(a,b,c)=myFunc(x,y,z)')
>
> # Get the process time. The timeit.clock() function would give me
> process time
> # but it doesn't allow me to access the return arguments afterward so
> I can't use it.
> # The syntax of the next line doesn't work but this is an example of
> what I hope to do.
> top_entry = p.getstats()[0]
> cum_time = top_entry[3]  # totaltime
>
> # Now, log all this info to a CSV file. (No help needed here.)
> csv_file.writerow([a,b,c,x,y,z,cum_time])
>
> Once this works, I will wrap the above code in a loop which iterates
> through thousands of combinations of the parameters x,y,z.
>
> Thanks in advance for your help.
>
> Reg

I think what you need to look at is the documentation for the various
Python profilers:

http://docs.python.org/library/profile.html

>From what I can see, it looks like cProfile can log to a file and you
use the pstats module to review the data generated. That link also
links to the hotshot profiler, which looks like it's even simpler to
use.

You might also want to check out the logging module.

-------------------
Mike Driscoll

Blog:   http://blog.pythonlibrary.org

PyCon 2010 Atlanta Feb 19-21      http://us.pycon.org/



More information about the Python-list mailing list