Why does stack.inspect take so long?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 63q2o4i02@sneakemail.com

    #1

    Why does stack.inspect take so long?

    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 disproportionat ely
    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

  • Tim Peters

    #2
    Re: Why does stack.inspect take so long?

    [63q2o4i02@sneak email.com][color=blue]
    > 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 disproportionat ely
    > 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?[/color]

    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,
    [color=blue][color=green][color=darkred]
    >>> import sys, inspect
    >>> def name_of_caller( ):[/color][/color][/color]
    .... return inspect.getfram einfo(sys._getf rame(1), context=0)[2][color=blue][color=green][color=darkred]
    >>> def f():[/color][/color][/color]
    .... print "my name is", name_of_caller( )[color=blue][color=green][color=darkred]
    >>> f()[/color][/color][/color]
    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.

    Comment

    • 63q2o4i02@sneakemail.com

      #3
      Re: Why does stack.inspect take so long?


      Tim Peters wrote:[color=blue]
      > [63q2o4i02@sneak email.com][color=green]
      > > Hi, I've written a top-down recursive decent parser for SPICE circuit
      > > descriptions. For debugging purposes, I wanted each production[/color][/color]
      ....[color=blue][color=green]
      > > Any clues?[/color]
      >[/color]
      [color=blue]
      > It should go much faster to use a function that doesn't crawl the
      > entire call stack. For example,
      >[color=green][color=darkred]
      > >>> import sys, inspect
      > >>> def name_of_caller( ):[/color][/color]
      > ... return inspect.getfram einfo(sys._getf rame(1), context=0)[2][color=green][color=darkred]
      > >>> def f():[/color][/color]
      > ... print "my name is", name_of_caller( )[color=green][color=darkred]
      > >>> f()[/color][/color]
      > 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.[/color]


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

      ms

      Comment

      Working...