8

While writing loops I often get confused with which one I should choose. For example,

int sum;
for(int i=0; i<10; i++)
{
   sum=0;
   ...
   ....
}

Or

for(int i=0; i<10; i++)
{
   int sum=0;
   ...
   ....
}

Say, the variable is only required in this loop. There is no need of it in the later part of the program. I need the value of the variable sum to be 0 at the beginning of the loop. Which one is the better practice? Re-initializing a variable at the start of the loop or re-declaring it? Which one is more efficient?

user2864740
  • 60,010
  • 15
  • 145
  • 220
anirban.at.web
  • 349
  • 2
  • 11
  • Thinking about it in terms of how I would convert that into assembly, they should be the same. But that's not to say the JVM will never do something stupid that messes it up and makes one of them less efficient. – Tyler Oct 15 '14 at 05:20
  • 5
    @KhairulBasar: Both are not equal. – ani627 Oct 15 '14 at 05:20
  • 6
    They have the *same* "efficiency" - declarations are *not* "executed" by the JVM and so do not incur any overhead as envisioned; the scope of `sum` is irrelevant to this discussion, being a local variable in either case, as the usage is not shown to be required later. (Although I encourage using the most restrictive scope possible.) – user2864740 Oct 15 '14 at 05:21
  • Please learn about `scope` of a variable. – Erran Morad Oct 15 '14 at 05:22
  • The compiler will move your declaration inside the loop if you don't use the variable past the loop, so from a performance perspective, they are exactly the same. – Voicu Oct 15 '14 at 05:25
  • Basically - don't worry about it. If there is a difference (and there probably isn't), it's going to be tiny. You've got better ways of spending your time. – Dawood ibn Kareem Oct 15 '14 at 05:27
  • @1336087: i meant both are equal in efficiency... – MD. Khairul Basar Oct 15 '14 at 05:32
  • Why don't you just run both versions and measure their execution time? – kraskevich Oct 15 '14 at 05:32
  • @user2040251 because that's harder than it sounds. You have to fight the optimizer to get useful info. – Tyler Oct 15 '14 at 05:33
  • 2
    Check out [this](http://stackoverflow.com/a/8878071) answer. – Dave Chen Oct 15 '14 at 05:35
  • possible duplicate of [Declaring variables inside or outside of a loop](http://stackoverflow.com/questions/8803674/declaring-variables-inside-or-outside-of-a-loop) – maaartinus Oct 15 '14 at 09:27

3 Answers3

8

If you declare a variable outside of the loop and not use it past the loop, the compiler will move the declaration inside the loop.

That means there is no reason to compare efficiency here, since you end up with the same exact code that the JVM will run for the two approaches.

So the following code:

int sum;
for(int i=0; i<10; i++)
{
   sum=0;
}

... becomes this after compilation:

for(int i = 0; i < 10; i++)
{
    int sum = 0;
}
Voicu
  • 16,921
  • 10
  • 60
  • 69
1

This leads us to concentrate on performance and optimization of code in looping concepts.

From a maintenance perspective, second case is better. Declare and initialize variables in the same place, in the narrowest scope possible. Don't leave a gaping hole between the declaration and the initialization.The scope of local variables should always be the smallest possible.See the link

Declaring variables inside or outside of a loop

Community
  • 1
  • 1
Anbu.Sankar
  • 1,326
  • 8
  • 15
0

Re initializing it within the loop will set the sum value to zero every time you start the loop, no need to re-declare. The answer is both have same efficiency.

sniperd
  • 5,124
  • 6
  • 28
  • 44
SanyTiger
  • 666
  • 1
  • 8
  • 23