Why does stack.inspect take so long?

63q2o4i02 at sneakemail.com 63q2o4i02 at sneakemail.com
Tue May 16 02:16:21 EDT 2006


Tim Peters wrote:
> [63q2o4i02 at sneakemail.com]
> > Hi, I've written a top-down recursive decent parser for SPICE circuit
> > descriptions.  For debugging purposes, I wanted each production
...
> >  Any clues?
>

> It should go much faster to use a function that doesn't crawl the
> entire call stack.  For example,
>
> >>> import sys, inspect
> >>> def name_of_caller():
> ...     return inspect.getframeinfo(sys._getframe(1), context=0)[2]
> >>> def f():
> ...     print "my name is", name_of_caller()
> >>> f()
> my name is f
>
> name_of_caller() takes time independent of the call-stack depth.
>
> The "context=0" is to avoid wasting time sucking up and packaging
> source-code lines you don't want anyway.


Cool, thanks.  I'll try that.   I didn't realize there were other ways
of getting it.

ms




More information about the Python-list mailing list