Why does stack.inspect take so long?

6

63q2o4i02

Hi, I've written a top-down recursive decent parser for SPICE circuit
descriptions. For debugging purposes, I wanted each production
rule/function to know what its own name was, so at the beginning of
each rule/function, I make a call to inspect.stack()[0][3] (I think...)
and that fetches the name from someplace. However, with a bunch of
test text input, this takes ~10 seconds to run. When I comment out the
inspect.stack() function from each production/function, the program
speeds up to 0.04s, or 250x !! I loaded up the profiler to see what
was there, and although I don't understand the output entirely, it
looks like the stack frame functions are used in disproportionately
large amounts compared to other often-used functions. This is a
*recursive* parser, so it's being called a lot even for simple things.
I'm wondering still why there is a 250x speed up when I comment it out.
Any clues?

thanks
ms
 
T

Tim Peters

[[email protected]]
Hi, I've written a top-down recursive decent parser for SPICE circuit
descriptions. For debugging purposes, I wanted each production
rule/function to know what its own name was, so at the beginning of
each rule/function, I make a call to inspect.stack()[0][3] (I think...)
and that fetches the name from someplace. However, with a bunch of
test text input, this takes ~10 seconds to run. When I comment out the
inspect.stack() function from each production/function, the program
speeds up to 0.04s, or 250x !! I loaded up the profiler to see what
was there, and although I don't understand the output entirely, it
looks like the stack frame functions are used in disproportionately
large amounts compared to other often-used functions. This is a
*recursive* parser, so it's being called a lot even for simple things.
I'm wondering still why there is a 250x speed up when I comment it out.
Any clues?

Look at the source code (inspect.py, function `stack()`). It
obviously takes time proportional to the total depth of the call
stack, and for a recursive-descent parser that's likely to be highly
non-trivial most of the time.

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.
 
6

63q2o4i02

Tim said:
[[email protected]]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top