Profiling Embedded Code

Alex Martelli aleax at aleax.it
Sat Mar 29 13:07:33 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.

Yes, but it's not defined in the __main__ module.

> 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.
> 
> Any suggestions for how to get this to work would be much appreciated,

If your module is named foo, use:

p.run('import foo; foo.UpdateGameData(0)')

or else use p.runctx instead, but the above may be simpler.


Alex





More information about the Python-list mailing list