» Home » VB Fibre
Site
News
Files

Visual Basic
Strings
Math
General
Properties
Memory
Methods

Search
Testing

Inline ASM-VB
Strings
Math
General
Memory

Search
Using inline ASM
Submit!

Static vs. Public variables

Sometimes I use Static variables, but I think I'm going for public now. Not that it really matters that much, but any performance increase is welcome for me. I've used the standard method for this test: 500.000 iterations, 5 times.

Used Code:

Declaration:
Private Var1 As Long

Code:
Public Sub TestOne() Static Var2 As Long Var2 = Var2 + 1 End Sub Public Sub TestTwo() Var1 = Var1 + 1 End Sub

Static % faster than Public var Static (sec) Public var (sec)
14,7% 0,009235 0,008050
33,1% 0,009499 0,007136
14,4% 0,008341 0,007291
12,2% 0,008427 0,007508
-1,1% 0,008130 0,008218


User contributed notes:

Author: Me (me at mail dot com) Date: 22:03 02/03/2005
Dude, you're using the static all wrong. This "test" proofs absolutely nothing as you're comparing totaly different functions and static uses and even comparing variant declarations with longs.

I'm not even going any deeper into this. Read a book about VB for a change...

Author: Almar () Date: 22:03 02/03/2005
or you should learn to read the code. There is no variant declaration there.

Author: GMan () Date: 08:04 04/04/2005
True dat.

Author: Tom (hurendo_kun at hotmail dot com) Date: 15:05 31/05/2005
I would like to mention, however, that your "public" variable isn't public at all. . . it's private to the local module. Memory space is handled differently on this level. I don't necessarily doubt the results, but you might want to do the test again with a true Public variable declared in a separate module, just to be sure.

Author: Mixael () Date: 17:02 21/02/2006

Since the static keyword means that the variable keeps its value between calls to the sub, and the public keyword means, essentially the same, then your test here does NOTHING to show the difference.

You need to st up a loop that calls the two subs several times to test them. (Unless you have code not shown...but then you should have shown that, too.) I can't see, from your presentation here, that you have used "Static" properly, nor do you have a "Public" variable (except while this Module is in scope). Therefore, this test is VOID for actual proof of ANYTHING.

Author: Almar () Date: 21:02 21/02/2006
Please take a closer look - There is a public variable. And the static value IS used. All tests are run multiple time, and ten-thousands of iterations as most other tests.

Unless I do not understand your message properly :)

Author: Mixael () Date: 23:02 26/02/2006
And I quote the source code:

Private Var1 As Long


As someone else noted before, you do NOT have a public variable. You have a loacal (module level) PRIVATE variable, easily seen from the PRIVATE keyword.


It can only be a public if it is declared public, and your test can only be "useful" if you call use the PUBLIC variable from another module. And as I said, unless you're not showing ALL of your code (and I doubt you are...no offense intended there), you declarations and results are suspect. Also, did you run from the IDE or from compiled code? These thigs all make a difference.

Author: Mixael () Date: 23:02 26/02/2006
Almost forgot:

Apologies for the delay between my first post and the second.

Also, for the timing...what methodd did you use for that? (Curiosity, nothing more)

Author: Almar () Date: 23:02 26/02/2006
The private variable is usuable from the function, and therefore simply valid?

Please check ot "Testing" at the left to see how testing was done :)

Author: sths () Date: 16:06 30/06/2006
shsht

Author: Spodi (spodii at hotmail dot com) Date: 14:08 31/08/2006
I dont see why you would even compair public VS static. If you can use static for the situation, then why the hell would you even make it public instead of private? I see nothing wrong with this test... the only reason I even understand why you guys could be complaining that he used a private variable instead of public is since he wrote public in the name of the test.

Add user-note
Author:
E-mail (optional):
Anti spam, please enter 'ok' without quotes:
Comment: