PyEval_SetProfile usage ?

Salman Malik salmanmk at live.com
Sat Jul 7 21:09:29 CEST 2012


Thanks Thomas! I will try the timer code that you provided. I have tried profiling with just wrapping the python code with cprofiler's runctx but the problem is that my python code is sort of a packet parser and is called for each packet, but when I see the profiled results  it appears that the profile gets rewritten for each packet and I can't see the aggregate result: I see 1-2 calls per function and each call is shown to have taken 0.00 time.

I have also tried valgrind's callgrind in conjunction with kcachegrind but it doesn't seem to show time measurement and by some reading I learned that it gives the CPU cycle's per function and not time. Any suggestions in this regard will also be very helpful.. Thanks!

Salman

> Date: Sat, 7 Jul 2012 11:50:05 +0200
> From: t at jollybox.de
> To: python-list at python.org
> Subject: Re: PyEval_SetProfile usage ?
> 
> On 07/06/2012 11:40 PM, Salman Malik wrote:
> > Hi All,
> > 
> > I have used the Python's C-API to call some Python code in my c code and
> > now I want to know how much time does my Python part of the program
> > takes. I came across the PyEval_SetProfile API and am not sure how to
> > use it. Do I need to write my own profiling function?
> 
> You could have a look at the source code of the cProfile extension
> module. I assume it uses the feature.
> 
> In your case, what's wrong with simply measuring the time?
> 
> 
> /* variables. */
> unsigned long delta_usec;
> struct timeval t1, t2;
> 
> /* get start time */
> gettimeofday (&t1, NULL);
> 
> /* DO PYTHON STUFF */
> 
> /* get end time */
> gettimeofday (&t2, NULL);
> 
> /* delta? */
> delta_usec = (t2.tv_sec - t1.tv_sec) * 1000
>            + (signed)(t2.tv_usec - t1.tv_usec);
> 
> /* if your Python code is called multiple times, do this for each one,
>    and (if you like) sum up the result. It's simpe enough, and you
>    don't have to mess with profiling function.
>    Alternatively, you could use a C profiler like Valgrind
> */
> -- 
> http://mail.python.org/mailman/listinfo/python-list
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120707/bb996ce8/attachment.html>


More information about the Python-list mailing list