Re: VB 6.0 Desktop - Global Variables




"Ulrich Korndoerfer" <ulrich_wants_nospam@xxxxxxxxxxxx> wrote in message
news:43EAA4C8.3000409@xxxxxxxxxxxxxxx
Hi,

MikeD schrieb:

...
So, the performance of global variables are slower than local variables.
Why? the extra-logic work that compiler has to do.


No. Performance should not be affected in any measurable way (from a
user perspective). Now, *memory* usage might be affected, but global
variables are not "slower" than local variables. As has been stated,
global variables tend to make maintainability much more difficult and,
IMO, make bugs much more likely (and more difficult to solve).


It depends on what one means with "global". If it is meant "having project
wide scope", then this kind of variables only can live in a module. Making
them local by giving them the attribute Private and giving acces by
property only or moving it into a class, then access will be (much)
slower!

Well, I consider a "global variable" (and even just "global" by itself) to
mean a variable declared using either the Global or Public keyword in a
standard code module. As you say, having project wide scope, but also
without needing a reference to an object. A variable that is declared
Private and has a property which publicly exposes it is not, IMO, global in
any way because it's encapsulated by the object. You need a reference to
that object to use it. If you don't have that reference, it's not
accessible. So how can that be considered global?

Now, as far as speed/performance:

I said "in any measurable way (from a user perspective)".

Sure, if you actually want to benchmark (done properly) any speed difference
in global variables vs. module-level or local variables or wrapping a
module-level variable in a property procedure, I suppose it is probably
measurable. The difference is going to be so small though that proper scope,
not to mention maintainability and all other benefits associated with using
the right scope (which goes hand-in-hand with writing good, clear, legible
code), is considerably more important than the few CPU cycles you'll save by
inundating your program with dozens or hundreds of global variables.

Everybody is so concerned with "speed". Sure, that's important and
something that needs to always be taken into consideration. But what's more
important (and ultimately is what really matters most) is "apparent speed"
from a user perspective.

--
Mike
Microsoft MVP Visual Basic


.



Relevant Pages

  • Re: finalize() newbie question
    ... that is not a newbie question at all. ... Although the reference variable "a" has gone out of scope in the ... at the binary level local variables aren't ...
    (comp.lang.java)
  • Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
    ... when in fact it's the other local variables that they (via the ... anonymous delegates) reference that are of concern. ... and "expensive" are tied in lifetime by their being in the same scope, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Private variables question
    ... already in use at a higher scope. ... but if those three lines reference a variable that is declared ... Local variables are always named and referenced with a lower case ... help in such cases even if you have a prefix. ...
    (borland.public.delphi.non-technical)
  • Re: missing include by compiler
    ... "Charles Russell" wrote: ... > block of code and reference it by name as in pascal. ... stay in scope due to host association and you can comment out the call ... In addition you can have local variables if you ...
    (comp.lang.fortran)
  • Re: vbs output to txt file
    ... the reference count to that object will be decremented. ... As does going out of scope:) ... You are religiously setting objects to nothing and not ... and so am seriously lacking in both scripting expertise ...
    (microsoft.public.scripting.wsh)