PyEval_SetProfile usage ?

Thomas Jollans t at
Sat Jul 7 11:50:05 CEST 2012

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);


/* 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

More information about the Python-list mailing list