Profiling Embedded Code

Mark Hammond mhammond at skippinet.com.au
Fri Mar 28 19:18:44 EST 2003


Andrew Wilkinson wrote:
> Hi,
> 
> I've got some Python code that is embedded into a C application, and I'd
> like to profile it. Unfortunatly I can't get the standard profile module to
> work.
> 
> My C code imports a module, and then at the appropriate time calls the
> UpdateGameData function. When the module is imported I'd like to do a 'test'
> run of the function, with the profiler on to perform a simple check on it.
> (I know this is not a particually good way to profile the code, but I'd like
> to get it working and then move on...)
> 
> The module is given (slightly trimmed) here...
> ---
> def UpdateGameData(time):
>     ...
> 
> 
> import profile
> p = profile.Profile()
> p.run('UpdateGameData(0)')
> --
> 
> With this code I get the following exception...
> Traceback (most recent call last):
>   File "./python/core.py", line 28, in ?
>     p.run('UpdateGameData(0)')
>   File "c:/programming/python/lib/profile.py", line 403, in run
>     return self.runctx(cmd, dict, dict)
>   File "c:/programming/python/lib/profile.py", line 409, in runctx
>     exec cmd in globals, locals
>   File "<string>", line 1, in ?
> NameError: name 'UpdateGameData' is not defined
> 
> Now this seems very silly to me as 'UpdateGameData' obviously is defined. I
> had a look in the code for profile and it seems to mess around with
> __main__, but I'm not sure that it's pointing at the right place.

__main__ is not what you want in this situation.  Try the "runcall" 
method in the profiler - you pass the function itself to this.

Mark.





More information about the Python-list mailing list