473,725 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static vs Dynamic Variables - Speed Difference

I have a function which is called from a loop many times. In that function,
I use three variables as counters and for other purposes. I can either use
DIM for declaring the variables or Static. Would the performance be better
using Static versus Dynamic. I would think it would be quicker with STATIC
declarations since the variables would only have to be created once. Can
anyone confirm this. Thanks.
--
Dennis in Houston
Nov 21 '05 #1
28 4632
It's not a matter of performance, it's a matter of whether you need the
values of those variables persisted between calls to the function in
question. If you do - use static. If you don't - use Dim.
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:9D******** *************** ***********@mic rosoft.com...
I have a function which is called from a loop many times. In that
function,
I use three variables as counters and for other purposes. I can either
use
DIM for declaring the variables or Static. Would the performance be
better
using Static versus Dynamic. I would think it would be quicker with
STATIC
declarations since the variables would only have to be created once. Can
anyone confirm this. Thanks.
--
Dennis in Houston

Nov 21 '05 #2
But strictly speaking, won't static produce less memory allocation code than
repeated calls to dim?

--
Jeff S.
"Scott M." <s-***@nospam.nosp am> wrote in message
news:eh******** ******@TK2MSFTN GP15.phx.gbl...
It's not a matter of performance, it's a matter of whether you need the
values of those variables persisted between calls to the function in
question. If you do - use static. If you don't - use Dim.
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:9D******** *************** ***********@mic rosoft.com...
I have a function which is called from a loop many times. In that
function,
I use three variables as counters and for other purposes. I can either
use
DIM for declaring the variables or Static. Would the performance be
better
using Static versus Dynamic. I would think it would be quicker with
STATIC
declarations since the variables would only have to be created once. Can
anyone confirm this. Thanks.
--
Dennis in Houston


Nov 21 '05 #3

"Dennis" <De****@discuss ions.microsoft. com> wrote
I have a function which is called from a loop many times. In that function,
I use three variables as counters and for other purposes. I can either use
DIM for declaring the variables or Static. Would the performance be better
using Static versus Dynamic. I would think it would be quicker with STATIC
declarations since the variables would only have to be created once. Can
anyone confirm this. Thanks.

Don't ry to second guess the compiler. Create and use variables as you need them
(least scope) and let the compiler decide how to optimize their use.

LFS
Nov 21 '05 #4
I wouldn't worry about your program's performance suffering because of this
choice. This will not be the bottleneck.

You should use what makes sense in the function. If the variable's value
does not need to be saved between function calls, then just dim the
variables.

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:9D******** *************** ***********@mic rosoft.com...
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. Would the performance be better using Static versus Dynamic. I would think it would be quicker with STATIC declarations since the variables would only have to be created once. Can
anyone confirm this. Thanks.
--
Dennis in Houston

Nov 21 '05 #5
Scott's argument is absolutely right - use static variables when you need
them otherwise stick to dimming the variables.

In addition, the CLR itself does not support static variables. This is
achieved by the VB compiler instead and this does incur a slight overhead -
not so much that you shouldn't use them even when you absolutely need to.
Here's a blog post that should explain why this overhead with static
variables:
http://weblogs.asp.net/psteele/articles/7717.aspx

The idea is that for normal programming, simply use local variables. Use
static variables only when you need them - when it indeed does make sense to
use them.
hope that helps..
Imran.
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:9D******** *************** ***********@mic rosoft.com...
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. Would the performance be better using Static versus Dynamic. I would think it would be quicker with STATIC declarations since the variables would only have to be created once. Can
anyone confirm this. Thanks.
--
Dennis in Houston

Nov 21 '05 #6
"Jeff Stewart" <ja*@micronovat ech.com> schrieb:
But strictly speaking, won't static produce less memory
allocation code than repeated calls to dim?


Why? If only one instance of your method is running, there will only be one
instance of the local variable.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #7
Jeff,
No Static will cause 2 or more class variables to be created, while (local)
Dim will cause a single variable stack based variable to be created.

If the method is called very rarely, you are "wasting" class variable space,
and if the variable is a Reference Type, you may also be "wasting" Heap
space.

For example look at the following class with ILDASM.EXE:

Public Class DynamicClass

Public Sub DoSomething()
Static SomeStaticValue As Integer = 100
Dim SomeLocalValue As Integer = 100
End Sub

End Class

You should see that SomeStaticValue caused two class level variables to be
created.

The first "$STATIC$DoSome thing$2001$Some StaticValue" is the value of the
Static variable.
The second "$STATIC$DoSome thing$2001$Some StaticValue$Ini t" is a special
class that indicates if the static value has been initialized yet or not.

You should also see that VB.NET added code to the constructor to initialize
the $STATIC$DoSomet hing$2001$SomeS taticValue$Init variable, plus it added a
whole lot of extra code to our method to support the Static Variable...

I totally agree & follow Scott M's comments when deciding to use Static over
Dim!

Hope this helps
Jay

"Jeff Stewart" <ja*@micronovat ech.com> wrote in message
news:1099683393 .4UNPUC0WXjL+DJ xWnHS5yg@terane ws...
But strictly speaking, won't static produce less memory allocation code
than repeated calls to dim?

--
Jeff S.
"Scott M." <s-***@nospam.nosp am> wrote in message
news:eh******** ******@TK2MSFTN GP15.phx.gbl...
It's not a matter of performance, it's a matter of whether you need the
values of those variables persisted between calls to the function in
question. If you do - use static. If you don't - use Dim.
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:9D******** *************** ***********@mic rosoft.com...
I have a function which is called from a loop many times. In that
function,
I use three variables as counters and for other purposes. I can either
use
DIM for declaring the variables or Static. Would the performance be
better
using Static versus Dynamic. I would think it would be quicker with
STATIC
declarations since the variables would only have to be created once.
Can
anyone confirm this. Thanks.
--
Dennis in Houston



Nov 21 '05 #8
The same number of instructions are created in each case. When entering a
function or sub procedure, the instruction SUB ESP, n is added to the start
of the function. "n" is computed at compile time to hold all (DIM)
variables needed by that function. Note that this instruction must also be
executed even if there are no variables because the stack frame pointer (BP)
must also be saved on the stack, so at mininum n must be 2 as BP is 2 bytes
long. Referencing variables on the stack frame or in global data storage
takes the same number of clock cycles, assuming the memory page for both is
already in memory.

Mike Ober.

"Jeff Stewart" <ja*@micronovat ech.com> wrote in message
news:1099688650 .NnV0SgOw/OuZ4gQFdZ+cag@t eranews...
In the dim scenario:
1) When the method is called, code must be executed to allocate storage
for the dim'd variable, which has local scope.
2) Leaving the method's scope can (should, I hope!) cause that dim'd
variable to be reclaimed. Subsequent calls to the method repeat step 1, and subsequent returns cause step 2 to be repeated.

In the static scenario:
1) The variable storage is allocated when the class is instantiated (or
the first time the method is called, depending on the compiler).
2) Each time the method is called (with the possible exception of the
first time, depending on the compiler), the variable's storage is -not-
reallocated -- memory allocation code is not necessary; it is the same as it was before. So step 1 above is not performed. Similarly, when leaving the method (so long as the class instance remains active), storage is not
reclaimed, so step 2 above is not performed.

It's probably a moot point, but it still seems to me like less instructions are executed in the latter scenario than the former. I'm sure that the
compilers out there have clever tricks for this kind of stuff, but since
most of my work is done in firmware, I worry about this stuff. :)

--
Jeff S.
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:O3******** ******@TK2MSFTN GP09.phx.gbl...
"Jeff Stewart" <ja*@micronovat ech.com> schrieb:
But strictly speaking, won't static produce less memory
allocation code than repeated calls to dim?


Why? If only one instance of your method is running, there will only be
one
instance of the local variable.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


Nov 21 '05 #9
Yes, but the values are held in memory potentially longer than using Dim.
"Jeff Stewart" <ja*@micronovat ech.com> wrote in message
news:1099683393 .4UNPUC0WXjL+DJ xWnHS5yg@terane ws...
But strictly speaking, won't static produce less memory allocation code
than repeated calls to dim?

--
Jeff S.
"Scott M." <s-***@nospam.nosp am> wrote in message
news:eh******** ******@TK2MSFTN GP15.phx.gbl...
It's not a matter of performance, it's a matter of whether you need the
values of those variables persisted between calls to the function in
question. If you do - use static. If you don't - use Dim.
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:9D******** *************** ***********@mic rosoft.com...
I have a function which is called from a loop many times. In that
function,
I use three variables as counters and for other purposes. I can either
use
DIM for declaring the variables or Static. Would the performance be
better
using Static versus Dynamic. I would think it would be quicker with
STATIC
declarations since the variables would only have to be created once.
Can
anyone confirm this. Thanks.
--
Dennis in Houston



Nov 21 '05 #10

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

Similar topics

1
2239
by: Tommy Lang | last post by:
I am trying to learn to use dynamic variables. I have pasted the code below. Is this the proper way of using dynamic variables? Thanks, Tommy //------------------------------------------------------------ #include <iostream>
2
1999
by: Joseph Turian | last post by:
When does initialization of static member variables occur? In the following code fragment, is m_start_time automatically initialized right before main()? Or is m_start_time uninitialized until the first time I use a static method in stats? Thanks
55
6231
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
2
4565
dmjpro
by: dmjpro | last post by:
i have a little bit confusion on interface and static members variables in it. having static member variables in interface and non-static member variables...........what is the difference? plz help. regards.
0
8872
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9392
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9091
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8069
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6694
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4505
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3211
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 we have to send another system
2
2619
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2150
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.