Re: VB 6.0 Desktop - Global Variables
- From: Ulrich Korndoerfer <ulrich_wants_nospam@xxxxxxxxxxxx>
- Date: Fri, 10 Feb 2006 01:46:30 +0100
Hi,
MikeD schrieb:
"Ulrich Korndoerfer" <ulrich_wants_nospam@xxxxxxxxxxxx> wrote in message news:43EAA4C8.3000409@xxxxxxxxxxxxxxxHi,
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?
Private variables don't need an object. Access to them via property does not need an object. A simple standard module suffices. So no reference to an object is needed to access them. And how it comes that you think that I think a private variable is global? Did you find some evidence for this in my post? Then please explain.
Now, as far as speed/performance:
I said "in any measurable way (from a user perspective)".
Heck, this is this old boring half breed argument. Of course the performance hit experienced by the user from *one* call will not show up. But think of eg a sorting routine (which are not so uncommon) accessing a memory location by global access or via a property (remember: this is a function call!) over and over again. *This* will show up to the user.
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.
Your'r kidding. "Probably measurable", lol. When you say supposing, it seemingly means you never did measure it. There are *big* differences. Remember again, properties are accessed via function calls.
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.
One will get apparent speed losses when using property access often.
--
Ulrich Korndoerfer
VB tips, helpers, solutions -> http://www.proSource.de/Downloads/
-----------------------------------------------------------------------
Plea for a bright future for VB classic.
Sign the petition to Microsoft: -> http://classicvb.org/petition/
-----------------------------------------------------------------------
.
- Follow-Ups:
- Re: VB 6.0 Desktop - Global Variables
- From: MikeD
- Re: VB 6.0 Desktop - Global Variables
- References:
- VB 6.0 Desktop - Global Variables
- From: isicoder
- Re: VB 6.0 Desktop - Global Variables
- From: Harvey Triana
- Re: VB 6.0 Desktop - Global Variables
- From: MikeD
- Re: VB 6.0 Desktop - Global Variables
- From: Ulrich Korndoerfer
- Re: VB 6.0 Desktop - Global Variables
- From: MikeD
- VB 6.0 Desktop - Global Variables
- Prev by Date: Re: GetPrivateProfileString() retrieves data but strips "", ????
- Next by Date: Re: How to run a program only once at startup or reboot?
- Previous by thread: Re: VB 6.0 Desktop - Global Variables
- Next by thread: Re: VB 6.0 Desktop - Global Variables
- Index(es):
Relevant Pages
|