Re: copymemory basic question
- From: "Tony Proctor" <tony_proctor@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 2 Sep 2005 10:11:43 +0100
Although I haven't checked what the VB compiler generates, 'static'
variables are generally slower in other languages, and for the same basic
reason: it's the way they're addressed rather than the way they're stored or
created.
When a procedure is called, a pre-amble usually allocates all the stack
space required for local stack-based variables (i.e. 'Dim' variables in VB).
Allocation of the space is done in one go, and tearing it down on procedure
exit is also done in one go. A bigger hit may be experienced with
initialised variables, especially objects, strings, and arrays. This is
because the code required to initialise them, and then deallocate their
dynamic memory on exit, is very significant. For a procedure in a Module,
the memory allocated for a static variable forms a permanent contribution to
the Thread Local Storage (TLS). For a procedure in a class, the memory is
allocated as part of the total aggregate required for class members and
static variables when each instance of that class is created.
When a stack variable is accessed, it's always via an offset-off-register
addressing mode, usually BP but sometimes SP if call frames have been
optimised out. This is a really quick addressing mode. For Static variables
in a Module, the variable is accessed by loading up it's 32-bit address and
referencing it directly. The instruction sequence to load the address is
bigger and uses both extra space and processor cycles. It would be possible
to use a global base register to locate TLS but that's considered expensive
as it locks down a register with a dedicated use. As for static variables in
a class, I'd be interested to know what code the VB compiler generates. It
should be efficient since it's accessing a location relative to the class
instance's memory aggregate, but then it doesn't seem to generate optimal
code in other cases.
Tony Proctor
"Karl E. Peterson" <karl@xxxxxxxx> wrote in message
news:uNYKNn1rFHA.904@xxxxxxxxxxxxxxxxxxxxxxx
> mscir wrote:
> > Have you seen this:
> >
> > http://www.aivosto.com/vbtips/not-optimize.html
> >
> > Static makes your app sooo static
> >
> > Did you know that Static local variables are slower than the normal
> > Dim locals? One could think that Statics are faster as they don't
> > need to get allocated every time you call the procedure. But it's not
> > so. Use Dim unless you have to do Static.
>
> Never had, no. Sure wish they offered some rationale for that, other than
just
> "because I said so." I've got an app here where it's clearly not the
case. On some
> machines, anyway. Would definitely be interesting to get to the bottom of
it!
> --
> Working Without a .NET?
> http://classicvb.org/petition
>
>
.
- Follow-Ups:
- Re: copymemory basic question
- From: Sam Hobbs
- Re: copymemory basic question
- From: Jim Mack
- Re: copymemory basic question
- References:
- Re: copymemory basic question
- From: Karl E. Peterson
- Re: copymemory basic question
- From: Donald Lessau
- Re: copymemory basic question
- From: Jim Mack
- Re: copymemory basic question
- From: Donald Lessau
- Re: copymemory basic question
- From: Karl E. Peterson
- Re: copymemory basic question
- From: Jim Mack
- Re: copymemory basic question
- From: Karl E. Peterson
- Re: copymemory basic question
- From: Jim Mack
- Re: copymemory basic question
- From: Karl E. Peterson
- Re: copymemory basic question
- From: Donald Lessau
- Re: copymemory basic question
- From: Karl E. Peterson
- Re: copymemory basic question
- From: mscir
- Re: copymemory basic question
- From: Karl E. Peterson
- Re: copymemory basic question
- Prev by Date: Re: copymemory basic question
- Next by Date: Re: copymemory basic question
- Previous by thread: Re: copymemory basic question
- Next by thread: Re: copymemory basic question
- Index(es):
Relevant Pages
|