|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Declaring variables inside loopsIs 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... Less efficient and also considered poor coding practice.
james Show quote "vbMental" <vbMen***@discussions.microsoft.com> wrote in message news:3C4698A0-2DB3-47C0-9B16-66640DA2F2DE@microsoft.com... > 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... Wow, I actually thought it was helpful especially if those variables were
only being used inside of the loop. Oh well, thanks. Show quote "james" wrote: > Less efficient and also considered poor coding practice. > james > > "vbMental" <vbMen***@discussions.microsoft.com> wrote in message news:3C4698A0-2DB3-47C0-9B16-66640DA2F2DE@microsoft.com... > > 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... > > > Hello james,
Do you have any data to back this up? Show quote > Less efficient and also considered poor coding practice. james > > "vbMental" <vbMen***@discussions.microsoft.com> wrote in message > news:3C4698A0-2DB3-47C0-9B16-66640DA2F2DE@microsoft.com... > >> 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... >> 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 Show quote "Matt Berther" <mbert***@hotmail.com> wrote in message news:7605369632413110928116470@news.microsoft.com... > 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" <vbMen***@discussions.microsoft.com> wrote in message >> news:3C4698A0-2DB3-47C0-9B16-66640DA2F2DE@microsoft.com... >> >>> 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... >>> > > > 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. Show quote > 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" <mbert***@hotmail.com> wrote in message > news:7605369632413110928116470@news.microsoft.com... > >> 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" <vbMen***@discussions.microsoft.com> wrote in message >>> news:3C4698A0-2DB3-47C0-9B16-66640DA2F2DE@microsoft.com... >>> >>>> 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... 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 Show quote "james" <jjames700ReMoVeMe at earthlink dot net> wrote in message news:eGz2TPn%23EHA.3124@TK2MSFTNGP11.phx.gbl... > Less efficient and also considered poor coding practice. > james > > "vbMental" <vbMen***@discussions.microsoft.com> wrote in message > news:3C4698A0-2DB3-47C0-9B16-66640DA2F2DE@microsoft.com... >> 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... > > 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. -- Show quoteGerry O'Brien Visual Basic.NET MVP "Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote in message news:%23%238m6zo%23EHA.2180@TK2MSFTNGP12.phx.gbl... >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%23EHA.3124@TK2MSFTNGP11.phx.gbl... >> Less efficient and also considered poor coding practice. >> james >> >> "vbMental" <vbMen***@discussions.microsoft.com> wrote in message >> news:3C4698A0-2DB3-47C0-9B16-66640DA2F2DE@microsoft.com... >>> 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... >> >> > > <"james" <jjames700ReMoVeMe at earthlink dot net>> wrote: Actually, identical efficiency and considered good coding practice, by > Less efficient and also considered poor coding practice. 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 - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too Uncle!!!!!!!! :-)
james Show quote "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MPG.1c5621f3c604a78b98bc21@msnews.microsoft.com... > <"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 - <sk***@pobox.com> > http://www.pobox.com/~skeet > If replying to the group, please do not mail me too 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" <vbMen***@discussions.microsoft.com> a écrit dans le message de news: 3C4698A0-2DB3-47C0-9B16-66640DA2F***@microsoft.com...Show quote > 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... |
|||||||||||||||||||||||