![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2008
Posts: 11
Rep Power: 0
![]() |
Is there any overhead declaring a variable inside a loop so it's in scope only in the loop?
|
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2007
Posts: 136
Rep Power: 2
![]() |
Re: Declaring variable inside loops?
Correct me if i'm wrong as C++ is not my area,
but initializing a variable inside a loop would simply re-initialize it to nothing every time around, so anything you do should be initialized before, or in c++ i think the syntax might be for(initialization;conditional;increment){ } like this: for(i = 0; i < 10; i++){
}usually you reference an incrementor in a for loop, which is handy for things like arrays or string indicies: string[i] |
|
|
|
|
|
#3 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5
![]() |
Re: Declaring variable inside loops?
Quote:
If you meant to add "relative to declaring it outside the loop" the answer is "it depends". It depends on the compiler (eg how it reuses variables, or folds two variables effectively into one, how it assigns variables to machine registers). It depends on how often the loop is executed (eg is it inside another loop, and can the variable be sensibly declared outside the enclosing loop?). There is also overhead of programmer effort to understand the code (eg code with a variable declared several pages before it is used is harder to understand - and therefore takes more effort to maintain - than code with declarations near the code that uses it). |
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Sep 2008
Location: Merimbula, Australia
Posts: 38
Rep Power: 0
![]() |
Re: Declaring variable inside loops?
Code-wise it is always considered 'cleaner' to keep your variables in scope only as long as you need them. So, if you only need the variable whilst in the loop it should be initialized in there too. I doubt you will notice any difference.
The good thing about doing it this way is that you save memory at runtime too. Consider an MMO of one million players. If each player was defined at global scope instead of when you need them it would add up to alot of ram.
__________________
A reflection on a pool of water does not reveal its depth... |
|
|
|
|
|
#5 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
Re: Declaring variable inside loops?
Quote:
// sample A
int foo;
for(int i = 0; i < someVal; i++)
{
foo = someInput.ReadInt();
foo /= 2;
someOutput.WriteInt(foo);
}
//sample b:
for(int i = 0; i < someVal; i++)
{
int foo = someInput.ReadInt();
foo /= 2;
someOutput.WriteInt(foo);
}
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Sep 2008
Location: Merimbula, Australia
Posts: 38
Rep Power: 0
![]() |
Re: Declaring variable inside loops?
I mean in general, it is better to limit scope in practise.
In a single loop it makes no difference whatsoever memory-wise.
__________________
A reflection on a pool of water does not reveal its depth... |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Oct 2008
Posts: 1
Rep Power: 0
![]() |
Re: Declaring variable inside loops?
I have a similar query to the one above: in my case I will be calculating several statistics per loop, and looping several thousand times. I've manipulated the example code above to reflect my situation, and included a third alternative where a single variable is continually reused:
// sample A: outside loop
float foo1;
float foo2;
float foo3;
float foo4;
float foo5;
for(int i = 0; i < someLargeVal; i++)
{
foo1 = someFunction1.ReadFloat();
foo2 = someFunction2.ReadFloat();
foo3 = someFunction3.ReadFloat();
foo4 = someFunction4.ReadFloat();
foo5 = someFunction5.ReadFloat();
someOutput.WriteFloat(foo1, foo2, foo3, foo4, foo5);
}
// sample B: inside loop
for(int i = 0; i < someLargeVal; i++)
{
float foo1 = someFunction1.ReadFloat();
float foo2 = someFunction2.ReadFloat();
float foo3 = someFunction3.ReadFloat();
float foo4 = someFunction4.ReadFloat();
float foo5 = someFunction5.ReadFloat();
someOutput.WriteFloat(foo1, foo2, foo3, foo4, foo5);
}
// sample C: reuse variable
float foo;
for(int i = 0; i < someLargeVal; i++)
{
foo = someFunction1.ReadFloat();
someOutput.WriteFloat(foo);
foo = someFunction2.ReadFloat();
someOutput.WriteFloat(foo);
foo = someFunction3.ReadFloat();
someOutput.WriteFloat(foo);
foo = someFunction4.ReadFloat();
someOutput.WriteFloat(foo);
foo = someFunction5.ReadFloat();
someOutput.WriteFloat(foo);
}I am very new to programming, but it seems to be that option C is the most efficient (and option B the worst) - i.e. that the overhead associated with creating/initializing N statistics * L thousand loops would be significant. I wonder if option A might be the most "safe"; it would definitely be the easiest for other programmers to read/understand (including myself down the line). I'd appreciate any advice. |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Sep 2008
Location: Merimbula, Australia
Posts: 38
Rep Power: 0
![]() |
Re: Declaring variable inside loops?
I would probably go with A as well.
The only disadvantage (which is minimal) is that your variables will be with you for the duration of the program (or at least the function which they are called from). Which means your program will consume a little more memory. Speed-wise though, I think it should be the best option. ![]()
__________________
A reflection on a pool of water does not reveal its depth... |
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5
![]() |
Re: Declaring variable inside loops?
I'd say it doesn't matter much between A,B, and C. We can easily see how to map between the different ways. Modern compiler vendors are fairly smart, and things like folding two variables into one or avoiding unnecessary allocation/deallocation of local variables are pretty elementary optimisations.
Better to go for readability - i.e. pick whichever of A, B, or C that is easiest to understand and maintain. If performance demands of your program are very tight, use a profiler to identify the code that needs to be lovingly hand-crafted. |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Aug 2008
Posts: 8
Rep Power: 0
![]() |
Re: Declaring variable inside loops?
Declaring variable inside loops limit the scope of variable to that loop . Once the loop is compelete it will again construct the same variable
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Declaring a Public Variable? | sharkbate24 | Visual Basic .NET | 2 | Aug 9th, 2007 3:18 AM |
| Is this declaring a variable? | waveform | C++ | 21 | Jun 23rd, 2006 4:48 PM |
| declaring a variable final | b1g4L | C++ | 5 | Jan 19th, 2006 12:41 PM |
| variable type | Writlaus | Java | 10 | Jan 11th, 2006 9:41 AM |
| variable problem | robert_sun | C | 1 | Apr 12th, 2005 3:10 PM |