[Numpy-discussion] line_profiler suggestion

Robert Kern robert.kern at gmail.com
Wed Jan 21 20:52:37 EST 2009


On Wed, Jan 21, 2009 at 15:44, Robert Kern <robert.kern at gmail.com> wrote:
> On Wed, Jan 21, 2009 at 15:35, Neal Becker <ndbecker2 at gmail.com> wrote:
>> Robert Kern wrote:
>>
>>> On Wed, Jan 21, 2009 at 15:13, Neal Becker <ndbecker2 at gmail.com> wrote:
>>>> Would be handy to not have to add/remove @profile to switch between
>>>> profiling/normal operation.
>>>>
>>>> When run without profiler, @profile is redefined to do nothing.
>>>> Possible?
>>>
>>> I could add a --noop flag to kernprof, which basically tells it to
>>> insert a do-nothing profile() decorator. I'm not sure what the use
>>> case is, though. Are you switching back and forth frequently?
>>>
>> That won't help, the idea is to not have to edit the code adding/removing
>> @profile in order to run _without_ kernprof.
>
> In your code, you can conditionally insert a profile() decorator into
> the __builtins__ if one isn't already there. You'll have to do that
> before you import anything.

Alternately, you could do this in a sitecustomize.py file, but then
your code will only work on your machine. All of these workarounds are
probably little, if any, improvement over the current situation. The
answer to your original question should probably be "No".

Perhaps a better workflow for you would be the capability to specify
the desired functions in, say, a text file rather than by modifying
the code. Currently, the trace hook determines whether or not to time
a line by looking at the code object it is from. The LineProfiler has
a set of the desired code objects and tests membership by identity.
The @profile decorator populates this set by grabbing the code objects
from the function objects. This is fast and robust.

It would be possible (but difficult to make robust) to instead look at
things like .co_filename and .co_name to determine whether or not to
time a line.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list