Declaring variables inside loops

G

Guest

Is it less efficient to declare variables inside loops like this:

For I = 1 to 1000000000000
Dim num as integer
........(using num here)......
Next I

Versus declaring outside the loop like this:

Dim num as integer
For I = 1 to 1000000000000
........(using num here)......
Next I


It looks as if the first one would keep re-allocating and releasing memory
for the variable...
 
G

Guest

Wow, I actually thought it was helpful especially if those variables were
only being used inside of the loop. Oh well, thanks.
 
C

Chris, Master of All Things Insignificant

I would not say that it is poor coding practice. If the var is only used in
the loop then it makes sense. We had a lengthy discussion about this a
month or two ago. If you look at the IL for both sets of code, it showed
there isn't a difference. I could be remembering that wrong (the majority
of the discussion was on the "with" operator) but I seem to remember it
saying there wasn't a disadvantage to doing the declare inside the loop. If
I get time tonight I'll get the IL code and post it.

Chris
 
G

Gerry O'Brien [MVP]

IL from first method;

..method private instance void Button1_Click(object sender,
class
[mscorlib]System.EventArgs e) cil managed
{
// Code size 22 (0x16)
.maxstack 2
.locals init ([0] int32 I,
[1] int32 num)
IL_0000: nop
IL_0001: ldc.i4.1
IL_0002: stloc.0
IL_0003: ldloc.1
IL_0004: ldc.i4.1
IL_0005: add.ovf
IL_0006: stloc.1
IL_0007: nop
IL_0008: ldloc.0
IL_0009: ldc.i4.1
IL_000a: add.ovf
IL_000b: stloc.0
IL_000c: ldloc.0
IL_000d: ldc.i4 0x5f5e100
IL_0012: ble.s IL_0003
IL_0014: nop
IL_0015: ret
} // end of method Form1::Button1_Click

IL from second method;

..method private instance void Button2_Click(object sender,
class
[mscorlib]System.EventArgs e) cil managed
{
// Code size 22 (0x16)
.maxstack 2
.locals init ([0] int32 num,
[1] int32 I)
IL_0000: nop
IL_0001: ldc.i4.1
IL_0002: stloc.1
IL_0003: ldloc.0
IL_0004: ldc.i4.1
IL_0005: add.ovf
IL_0006: stloc.0
IL_0007: nop
IL_0008: ldloc.1
IL_0009: ldc.i4.1
IL_000a: add.ovf
IL_000b: stloc.1
IL_000c: ldloc.1
IL_000d: ldc.i4 0x5f5e100
IL_0012: ble.s IL_0003
IL_0014: nop
IL_0015: ret
} // end of method Form1::Button2_Click

Not much difference at all. Personally I have an issue with declaring a
variable every time a loop executes however, you can make use of the fact
that the variable declared inside the loop, is not available outside the
loop therefore naming conflicts will not exist.

Keep in mind that this is a simple data type. If you start using object
declarations in this way, there could be performance and memory issues
because the GC is non-deterministic.
 
J

james

Hi Matt, no, I have no actual data to back this up. My main problem with the practice of declaring a variable inside a loop
comes from debugging problems that I have found with other programmers code
and my own when I have tired doing that. I also find code much more readable(personal opinion) by putting declarations either
at the top of a sub, or if they are public to a Class, at the top of the class.
And that was the way I re-learned to do it,,,,,,,,,,,,,,,,,after finding problems doing it the other way.
but, to each his own. And in places I have worked, declaring variables etc. inside a loop were/are a no-no.
james
 
M

Matt Berther

Hello james,

I can see you're point about saying that its poor coding practice (although
I will disagree with you). In my opinion, I want to see the variable defined
as close to its use as possible. My memory doesnt work very well, and if
they are declared at the top, I've probably forgotten what they are by the
time I look at them in context. ;)

I wanted to call you on your comment that they were less efficient. As demonstrated
by the other posts in this thread, the IL generated is virtually identical,
which means the efficiency is the same.

This means that the answer to the original poster's question comes down to
personal preference.
 
B

Bruno Jouhier [MVP]

I thought that the recommended practice was to try to reduce the scope of
variables as much as possible. So, if a variable is only used by the body of
a loop, it should be declared inside the loop rather than outside. At least,
this is the guideline that we use. The main advantage is for code
maintenance: you don't have to scan the entire method to investigate where
the variable is used, you immediately know that it is local to the loop.

It does not make any difference in terms of performance, just look at the
IL.

Bruno.
 
J

Jon Skeet [C# MVP]

Less efficient and also considered poor coding practice.

Actually, identical efficiency and considered good coding practice, by
limiting the scope of the variable to only what is required. (It makes
it obvious that the variable won't be used elsewhere, and allows
another local variable of the same name to be declared in a similar
scope later, which can often be very handy.)
 
J

james

Uncle!!!!!!!! :)

james

Jon Skeet said:
Actually, identical efficiency and considered good coding practice, by
limiting the scope of the variable to only what is required. (It makes
it obvious that the variable won't be used elsewhere, and allows
another local variable of the same name to be declared in a similar
scope later, which can often be very handy.)
 

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