Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instances and iterations. Which one is better?

I wanna read documents and process them. Each iteration processes one document.

Which kind of code is better?

1.

BufferedReader br;
for(File f : files)
{
    br = new BufferedReader(......);
    ......
}

2.

for(File f : files)
{
    BufferedReader br = new BufferedReader(......);
    ......
}

My point is which one is more efficient in terms of space and speed?

like image 492
Munichong Avatar asked Nov 19 '12 19:11

Munichong


4 Answers

The latter is clearer in my view. In general, prefer to declare local variables with the smallest scope you can, ideally initializing them at the point of declaration.

It won't directly affect the performance - but it will affect readability and maintenance, which will affect the ease with which you can make changes which will affect performance.

In general:

  • Work out your performance and behavioural requirements (and how you'll test both)
  • Write the simplest, cleanest code which achieves the behaviour you'll want
  • See if it meets your performance requirements
  • If not, analyze where the problem is, and make the "least unclean" change to improve things. (This may mean making a design change rather than a micro-optimization.)
  • Lather, rinse, repeat until you've got code which meets your performance requirements and is as clean as it can be.
like image 125
Jon Skeet Avatar answered Oct 14 '22 05:10

Jon Skeet


  1. They are exactly the same and all differences are lost after compiling into bytecode.
  2. Even if we imagine the worst possible scenario, which could theoretically (in another Java) amount to one extra memory write, the difference would be so ridiculously small that you'd need the world's most precise atomic clock to measure it.
  3. The real difference for you should lie with the overall maintainability of your code and that should be a much higher concern than even a real difference in speed lower than 2-3%. For example, many design patterns introduce some kind of overhead, but people are more than willing to pay that for the amount of flexibility it gives the code base.

In code optimization it is very easy to fall into the trap of Win Small, Lose Big: each individual method may be optimized to perfection, but overall system performance may still be a disaster due to the inadequacies of global architecture. One must always optimize top-down, and when doing so, you'll see that only 1% or less lines of code can actually contribute to overall performance if replaced by an optimized version.

like image 31
Marko Topolnik Avatar answered Oct 14 '22 06:10

Marko Topolnik


If you don't use the br variable elsewhere, they're both exactly the same.

Beware not to lose too much time attempting nano-optimizations. Even when the bytecode differs, the JIT isn't so bad when optimizing the obvious. You don't need to declare a variable before the block where it is used and you shouldn't as it make it less clear what it is used for.

like image 38
Denys Séguret Avatar answered Oct 14 '22 06:10

Denys Séguret


The only difference is the br is second case is local in scope to loop whereas in first case it can be accessed outside loop. But note that even in first case br ref variable would be available outaide loop and not the values you gave it inside for loop.

Else they are just the same. Though the second case is more readable

like image 32
Mukul Goel Avatar answered Oct 14 '22 06:10

Mukul Goel