473,480 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

May 16 '06 #1
2 2193
[63*******@sneakemail.com]
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.
May 16 '06 #2

Tim Peters wrote:
[63*******@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

May 16 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
3210
by: Bamber | last post by:
Why does f get returned ? (compiled with gcc). #include<stdio.h> int func(int a); main() { int f; f=func(7); printf("f=%d",f);
89
5944
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
12
6197
by: Veeru | last post by:
Hi, Can anyone tell me the difference between heap memory and stack stack both physically and fundamentally. It would be a real favour. Regards, Veeru
13
2058
by: jm.suresh | last post by:
Hi, I have a program which literately finds the object that overlapping a point. The horizontal and vertical search are called recursively from inside each other. Is this way of implementation...
10
11288
by: giddy | last post by:
hi , I' know what the StructLayoutAttribute does. I've seen it being used. But what does the Pack field do!? This is what MSDN says: Controls the alignment of data fields of a class or...
1
7075
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
2
1623
by: Julien | last post by:
Hello all, I would like to do something like: def called(arg) if arg==True: !!magic!!caller.return 1 def caller(arg) called(arg)
3
1717
by: Rafe | last post by:
Hi, I think I have discovered two bugs with the inspect module and I would like to know if anyone can spot any traps in my workaround. I needed a function which takes a function or method and...
0
2182
by: rajasankar | last post by:
Hi, I am using Jython based application and trying to use inspect.py in the python files. Here is my code import inspect,os,sys,pprint,imp def handle_stackframe_without_leak(getframe): ...
0
7046
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7088
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5342
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4783
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2997
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
183
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.