[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. -- http://mail.python.org/mailman/listinfo/python-list