PC Review


Reply
Thread Tools Rate Thread

Declaring variables inside loops

 
 
=?Utf-8?B?dmJNZW50YWw=?=
Guest
Posts: n/a
 
      14th Jan 2005
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...
 
Reply With Quote
 
 
 
 
james
Guest
Posts: n/a
 
      14th Jan 2005
Less efficient and also considered poor coding practice.
james

"vbMental" <(E-Mail Removed)> wrote in message news:3C4698A0-2DB3-47C0-9B16-(E-Mail Removed)...
> 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...



 
Reply With Quote
 
=?Utf-8?B?dmJNZW50YWw=?=
Guest
Posts: n/a
 
      14th Jan 2005
Wow, I actually thought it was helpful especially if those variables were
only being used inside of the loop. Oh well, thanks.

"james" wrote:

> Less efficient and also considered poor coding practice.
> james
>
> "vbMental" <(E-Mail Removed)> wrote in message news:3C4698A0-2DB3-47C0-9B16-(E-Mail Removed)...
> > 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...

>
>
>

 
Reply With Quote
 
Matt Berther
Guest
Posts: n/a
 
      14th Jan 2005
Hello james,

Do you have any data to back this up?

--
Matt Berther
http://www.mattberther.com

> Less efficient and also considered poor coding practice. james
>
> "vbMental" <(E-Mail Removed)> wrote in message
> news:3C4698A0-2DB3-47C0-9B16-(E-Mail Removed)...
>
>> 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...
>>



 
Reply With Quote
 
Chris, Master of All Things Insignificant
Guest
Posts: n/a
 
      14th Jan 2005
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



"james" <jjames700ReMoVeMe at earthlink dot net> wrote in message
news:eGz2TPn%(E-Mail Removed)...
> Less efficient and also considered poor coding practice.
> james
>
> "vbMental" <(E-Mail Removed)> wrote in message
> news:3C4698A0-2DB3-47C0-9B16-(E-Mail Removed)...
>> 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...

>
>



 
Reply With Quote
 
Gerry O'Brien [MVP]
Guest
Posts: n/a
 
      15th Jan 2005
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.

--
Gerry O'Brien
Visual Basic.NET MVP


"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote
in message news:%23%238m6zo%(E-Mail Removed)...
>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
>
>
>
> "james" <jjames700ReMoVeMe at earthlink dot net> wrote in message
> news:eGz2TPn%(E-Mail Removed)...
>> Less efficient and also considered poor coding practice.
>> james
>>
>> "vbMental" <(E-Mail Removed)> wrote in message
>> news:3C4698A0-2DB3-47C0-9B16-(E-Mail Removed)...
>>> 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...

>>
>>

>
>



 
Reply With Quote
 
james
Guest
Posts: n/a
 
      15th Jan 2005
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

"Matt Berther" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> Hello james,
>
> Do you have any data to back this up?
>
> --
> Matt Berther
> http://www.mattberther.com
>
>> Less efficient and also considered poor coding practice. james
>>
>> "vbMental" <(E-Mail Removed)> wrote in message
>> news:3C4698A0-2DB3-47C0-9B16-(E-Mail Removed)...
>>
>>> 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...
>>>

>
>
>



 
Reply With Quote
 
Matt Berther
Guest
Posts: n/a
 
      15th Jan 2005
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.

--
Matt Berther
http://www.mattberther.com

> 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
>
> "Matt Berther" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> Hello james,
>>
>> Do you have any data to back this up?
>>
>> --
>> Matt Berther
>> http://www.mattberther.com
>>> Less efficient and also considered poor coding practice. james
>>>
>>> "vbMental" <(E-Mail Removed)> wrote in message
>>> news:3C4698A0-2DB3-47C0-9B16-(E-Mail Removed)...
>>>
>>>> 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...



 
Reply With Quote
 
Bruno Jouhier [MVP]
Guest
Posts: n/a
 
      15th Jan 2005
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.

"vbMental" <(E-Mail Removed)> a écrit dans le message de
news: 3C4698A0-2DB3-47C0-9B16-(E-Mail Removed)...
> 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...



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      17th Jan 2005
<"james" <jjames700ReMoVeMe at earthlink dot net>> wrote:
> 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.)

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Declaring variables in loops Marc Gravell Microsoft C# .NET 1 23rd May 2007 09:55 PM
Declaring variables in loops =?Utf-8?B?QUw=?= Microsoft C# .NET 6 23rd May 2007 07:43 PM
Re: Declaring variables in loops Marc Gravell Microsoft C# .NET 1 23rd May 2007 07:38 PM
Re: Declaring variables in loops Marc Gravell Microsoft C# .NET 0 23rd May 2007 07:16 PM
declaring variables =?Utf-8?B?c2tpZ2Fs?= Microsoft Access Queries 5 2nd Apr 2007 01:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:35 PM.