Declaration Scope and Efficiency

J

JimM

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 ---
 
H

Herfried K. Wagner [MVP]

* "JimM said:
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>
 
F

Fergus Cooney

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
 
J

Jay B. Harlow [MVP - Outlook]

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/default.asp?url=/library/en-us/dndotnet/html/fastmanagedcode.asp

http://msdn.microsoft.com/library/d...y/en-us/dndotnet/html/highperfmanagedapps.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/vbnstrcatn.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchperfopt.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetperftechs.asp

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top