Help getting profile information programmatically

S

sawilla

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
 
M

Mike Driscoll

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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,966
Members
47,513
Latest member
JeremyLabo

Latest Threads

Top