Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 19th, 2008, 4:16 AM   #1
Goblin
Newbie
 
Goblin's Avatar
 
Join Date: Aug 2008
Posts: 11
Rep Power: 0 Goblin is on a distinguished road
Question Declaring variable inside loops?

Is there any overhead declaring a variable inside a loop so it's in scope only in the loop?
Goblin is offline   Reply With Quote
Old Sep 19th, 2008, 4:50 AM   #2
MiKuS
Hobbyist Programmer
 
Join Date: Jun 2007
Posts: 136
Rep Power: 2 MiKuS is on a distinguished road
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++){

}
changing the value of i inside the loop will result in an infinate loop.
usually you reference an incrementor in a for loop, which is handy for things like arrays or string indicies:
string[i]
MiKuS is offline   Reply With Quote
Old Sep 19th, 2008, 5:44 AM   #3
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5 grumpy will become famous soon enough
Re: Declaring variable inside loops?

Quote:
Originally Posted by Goblin View Post
Is there any overhead declaring a variable inside a loop so it's in scope only in the loop?
Of course it does. Any variable definition comes with some overhead.

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).
grumpy is offline   Reply With Quote
Old Sep 20th, 2008, 4:29 AM   #4
lonewolff
Programmer
 
lonewolff's Avatar
 
Join Date: Sep 2008
Location: Merimbula, Australia
Posts: 38
Rep Power: 0 lonewolff is on a distinguished road
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...
lonewolff is offline   Reply With Quote
Old Sep 20th, 2008, 5:45 AM   #5
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
Re: Declaring variable inside loops?

Quote:
Originally Posted by lonewolff View Post
The good thing about doing it this way is that you save memory at runtime too.
How so? You'll need the memory either way. Take for example:
// 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);
}
In sample A, you use the same int for each operation. From a very simplified view, you would create a stack variable once and reuse the memory location for each loop iteration. Sample B, however, would cause you to push a new variable onto the stack and later pop it off for each iteration. Now, this is a vastly over-simplified model, but it's probably possible to write code where this will be true; however, in most cases, the compiler will probably be able to make the best choice on how to create the variables, how to reuse the space, and even whether or not to save them on the stack or do all the work in registers (this goes back to what grumpy posted).
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Sep 20th, 2008, 6:03 AM   #6
lonewolff
Programmer
 
lonewolff's Avatar
 
Join Date: Sep 2008
Location: Merimbula, Australia
Posts: 38
Rep Power: 0 lonewolff is on a distinguished road
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...
lonewolff is offline   Reply With Quote
Old Oct 7th, 2008, 10:12 AM   #7
nova
Newbie
 
nova's Avatar
 
Join Date: Oct 2008
Posts: 1
Rep Power: 0 nova is on a distinguished road
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.
nova is offline   Reply With Quote
Old Oct 7th, 2008, 4:29 PM   #8
lonewolff
Programmer
 
lonewolff's Avatar
 
Join Date: Sep 2008
Location: Merimbula, Australia
Posts: 38
Rep Power: 0 lonewolff is on a distinguished road
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...
lonewolff is offline   Reply With Quote
Old Oct 8th, 2008, 3:26 AM   #9
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5 grumpy will become famous soon enough
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.
grumpy is offline   Reply With Quote
Old Oct 8th, 2008, 5:52 AM   #10
vikas1234
Newbie
 
Join Date: Aug 2008
Posts: 8
Rep Power: 0 vikas1234 is on a distinguished road
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
vikas1234 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC