473,320 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Declaration Scope and Efficiency

I'm curious if anyone knows what the most appropriate way of coding the
following situation:

(Version 1)
Dim x As Integer
For i As Integer = 0 To iLimit
For j As Integer = 0 To jLimit
x = SomeFunction(i,j)
DoSomethingHere(x,i)
If x > 3 Then
DoSomeThingElse(x,j)
End If
Next j
Next i

(Version 2)
For i As Integer = 0 To iLimit
For j As Integer = 0 To jLimit
Dim x As Integer = SomeFunction(i,j)
DoSomethingHere(x,i)
If x > 3 Then
DoSomeThingElse(x,j)
End If
Next j
Next i

Despite former BASIC programming practices, I'm preferring to code these
days with tightly scoped variables when I can (for example, x above).

The second is my preferable approach, but I'm curious if by declaring the
variable within the loops I would incur a performance penalty. Any ideas?

--- Jim ---
Nov 20 '05 #1
3 1103
* "JimM" <no*****@hotmail.com> scripsit:
I'm curious if anyone knows what the most appropriate way of coding the
following situation:

(Version 1)
Dim x As Integer
For i As Integer = 0 To iLimit
For j As Integer = 0 To jLimit
x = SomeFunction(i,j)
DoSomethingHere(x,i)
If x > 3 Then
DoSomeThingElse(x,j)
End If
Next j
Next i

(Version 2)
For i As Integer = 0 To iLimit
For j As Integer = 0 To jLimit
Dim x As Integer = SomeFunction(i,j)
DoSomethingHere(x,i)
If x > 3 Then
DoSomeThingElse(x,j)
End If
Next j
Next i

Despite former BASIC programming practices, I'm preferring to code these
days with tightly scoped variables when I can (for example, x above).

The second is my preferable approach, but I'm curious if by declaring the
variable within the loops I would incur a performance penalty. Any ideas?


Have a look at the IL code with "ildasm.exe"

;-)

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Improve your quoting style:
<http://learn.to/quote>
<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #2
Hi Jim,

If this is the inner loop of your program and you are anticipating huge
savings then all attention should be put into optimisation.

If this is one of the run-of-the mill routines, then the emphasis should
be on effective coding, readability, robustness and all those
non-nanosecond-aware parts of the art of programming.

In this case of declarations, the Dim is for a reservation on the stack.
And reservatios only need to be made once. It doesn't matter where it is
placed in that sense. The important aspect is the scope. Like you, I prefer
declarations that minimise the scope.

The things to be aware of, and I'm probably just reminding you, is that a
declaration that uses New within a loop will create a new object each time.
And often that's just what is required.

If you do a search on the word Optimisation (maybe with a 'z') and
sender/author Jay, he has given a bunch of links to MSDN articles on
optimisation techniques in .NET.

Regards,
Fergus

Nov 20 '05 #3
Jim,
As Herfried suggested, look at the IL.

However I tend to feel write your programs in the most straight forward
manor first, if x is only applicable to the inside of the loop, then put x
inside the loop.

At a later time, when your routine is proven to have a performance issue,
then you should look at optimizing that routine.

In other words don't worry about writing optimized code up front, worry
about writing good (OOP) code up front. Then only optimize code that is
found to have a problem.

The following articles, which Fergus referred to, provide information on
writing .NET code that performs well.

http://msdn.microsoft.com/library/de...anagedcode.asp

http://msdn.microsoft.com/library/de...anagedapps.asp

http://msdn.microsoft.com/library/de...vbnstrcatn.asp

http://msdn.microsoft.com/library/de...tchperfopt.asp

http://msdn.microsoft.com/library/de...tperftechs.asp

Hope this helps
Jay
"JimM" <no*****@hotmail.com> wrote in message
news:Of**************@TK2MSFTNGP11.phx.gbl...
I'm curious if anyone knows what the most appropriate way of coding the
following situation:

(Version 1)
Dim x As Integer
For i As Integer = 0 To iLimit
For j As Integer = 0 To jLimit
x = SomeFunction(i,j)
DoSomethingHere(x,i)
If x > 3 Then
DoSomeThingElse(x,j)
End If
Next j
Next i

(Version 2)
For i As Integer = 0 To iLimit
For j As Integer = 0 To jLimit
Dim x As Integer = SomeFunction(i,j)
DoSomethingHere(x,i)
If x > 3 Then
DoSomeThingElse(x,j)
End If
Next j
Next i

Despite former BASIC programming practices, I'm preferring to code these
days with tightly scoped variables when I can (for example, x above).

The second is my preferable approach, but I'm curious if by declaring the
variable within the loops I would incur a performance penalty. Any ideas?

--- Jim ---

Nov 20 '05 #4

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

Similar topics

7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
8
by: Alex Vinokur | last post by:
=========== Windows 2000 Intel C++ 8.0 =========== ------ foo.cpp ------ int main () { for (int i = 0; i < 10; i++); for (int i = 0; i < 10; i++);
12
by: Michael B Allen | last post by:
Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used? For example; void fn(struct foo *f, int bar) {...
15
by: main() | last post by:
Hi all, When i compile following piece of code, # include <stdio.h> void fun(int val) { int val; /*problem is here*/ printf("%d\n",val);
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
1
by: INeedADip | last post by:
What is the difference between: function setupGrid( param ){......} and setupGrid = function( param ){......} Are there any advantages to doing one over the other?
4
by: the_init | last post by:
Hi friends, Can you please explain me why the following code prints 10, instead of 20 - Here I am little confused with the static and global declaration of i in the same program. ...
6
by: WaterWalk | last post by:
I find friend declaration just very tricky. I tried the following examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are surprising. Example1: namespace ns1 { class Test { friend...
3
by: Victor Bazarov | last post by:
jl_post@hotmail.com wrote: Yes, certainly. Actually 'delete' is an operator, not a function. The parens are superfluous. There is really no need for that.
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.