aspose file tools*
The moose likes Beginning Java and the fly likes Performance!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Performance!!" Watch "Performance!!" New topic
Author

Performance!!

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Hello Everybody,

I am bit confused and try to find out which is the best way of Initializing the variable sum here. My questions are:

1. If I define the second way, does it mean it will put more load on the system.

2. does the efficiency get reduce in second method, as it is Initializing every time.

Please help me to find a answer.

Thanks & regards

Arun

Code I:

Code II:

[ March 27, 2006: Message edited by: arun mahajan ]
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
I don't know about the efficiency, although I suspect it is negligible in a simple example. The recursive version executes far slower, so it might affect that in a negative way.

The second is just plain bad programming, regardless of efficiency. Why re-declare the same variable that is used over and over? It is pointless and illogical, don't do it.
[ March 27, 2006: Message edited by: Rusty Shackleford ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Ashish Chopra
Ranch Hand

Joined: Nov 30, 2004
Posts: 134
It is always a good practise to re-use as many variables as possible in a program, as initializing new variables always ends up making more and more use of the heap..which is not a good practise if you don't need the new variables.

So in the second case, you are definitely wasting heap memory, and if "sum" were to be an object, you;d end up creating a large number of redundant objects.

Although you will not be putting too much extra load in terms of execution time on your PC, but as I said, if sum were an object, you could end up slowing your program down, due to too many objects.


Quis Custodiet Ipsos Custodes<br /> <br />My blog: <a href="http://www.coherentrambling.blogspot.com" target="_blank" rel="nofollow">http://www.coherentrambling.blogspot.com</a>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41574
    
  54
It is always a good practise to re-use as many variables as possible in a program

Absolutely not. If you had said something like "If utmost runtime performance is the only concern, and readability and maintainability of the code does not matter at all, then re-use of variables is a good thing", then one could argue about it. But not always, and it's certainly not good practice.


Ping & DNS - my free Android networking tools app
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Rusty]: The recursive version executes far slower, so it might affect that in a negative way.

What recursive version? I don't see any recursion in either code sample.

If you think one of those bits of code executes slower than the other, I suggest testing it. I believe you will find they both perform the same. Even "if utmost runtime performance is the only concern" - there is no performance penalty for declaring a local variable inside a loop. It performs the same as if you had declared it outside.

There is a tiny chance that someone may one day make a compiler which behaves differently, but every time this subject has come up in the past and someone studied the byte codes, we confirmed that the compiler generates the same byte codes (or effectively the same byte codes) in each case. I would say it's very unlikely that will change anytime soon. But feel free to test this yourself to confirm.

[Rusty]: The second is just plain bad programming, regardless of efficiency. Why re-declare the same variable that is used over and over? It is pointless and illogical, don't do it.

[Ashish]: It is always a good practise to re-use as many variables as possible in a program, as initializing new variables always ends up making more and more use of the heap..


I strongly disagree with both of these statements. These are local variables, therefore on the stack rather than the heap. If you study the bytecode generated you will find that there's no reallocation occurring - the compiler just keeps reusing the same memory space each time through the loop. So it costs you nothing to redeclare the variable. More important are two other effects: (1) by eclaring a variable as clase as possible to where it's actually used, you make it easier to understand that code. And (2) by restricting the scope of the variable to the minimum required (declare the variable as late as possible, inside blocks if possible), you typically would decrease the chance of errors which might occur from accidentally re-using a variable name somewhere else. Many of us have loop variables named i, or Iterators named it or iter - if you have two loops one after another, and in the second loop you type i when you should have said i2 (or "iter" when you should have typed "iter2", you get a bug. Such bugs are easily avoided (or promptly detected) if each loop variable is only defined within the loop it's needed for.

I suggest reading Effective Java, Item 29: Minimize the scope of local variables. Also the rest of the book is strongly recommended, for other reasons.


"I'm not back." - Bill Harding, Twister
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
I also, strongly recommend effective java. It is a great book for someone with 2+ years of experience.


Both your programs are pretty straight forward and should not make any difference in terms of performance.


java j2ee job interview questions with answers | Learn the core concepts and the key areas
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Ashish Chopra:
So in the second case, you are definitely wasting heap memory, and if "sum" were to be an object, you;d end up creating a large number of redundant objects.


Variables never are objects. They are either primitives or references. The number of objects created on the heap has nothing to do at all with how many local variables you declare.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jim Yingst:
by declaring a variable as clase as possible to where it's actually used, you make it easier to understand that code.


Additionally, by reducing the scope of a variable as much as possible, the compiler actually can decide to reuse the memory space for another variable later on.
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Originally posted by Jim Yingst:
[Rusty]: The recursive version executes far slower, so it might affect that in a negative way.

What recursive version? I don't see any recursion in either code sample.

If you think one of those bits of code executes slower than the other, I suggest testing it. I believe you will find they both perform the same. Even "if utmost runtime performance is the only concern" - there is no performance penalty for declaring a local variable inside a loop. It performs the same as if you had declared it outside.

There is a tiny chance that someone may one day make a compiler which behaves differently, but every time this subject has come up in the past and someone studied the byte codes, we confirmed that the compiler generates the same byte codes (or effectively the same byte codes) in each case. I would say it's very unlikely that will change anytime soon. But feel free to test this yourself to confirm.



I never said that he had a recursive version. Perhaps I should have said 'A'instead of 'The' to avoid simple misunderstandings.

I also did not say if it was a performance penalty, but I did say it was pointless. I would like to see arguments why someone should write it like the second part.

[b][Rusty]: The second is just plain bad programming, regardless of efficiency. Why re-declare the same variable that is used over and over? It is pointless and illogical, don't do it.



I strongly disagree with both of these statements. These are local variables, therefore on the stack rather than the heap. If you study the bytecode generated you will find that there's no reallocation occurring - the compiler just keeps reusing the same memory space each time through the loop. So it costs you nothing to redeclare the variable. More important are two other effects: (1) by eclaring a variable as clase as possible to where it's actually used, you make it easier to understand that code. And (2) by restricting the scope of the variable to the minimum required (declare the variable as late as possible, inside blocks if possible), you typically would decrease the chance of errors which might occur from accidentally re-using a variable name somewhere else. Many of us have loop variables named i, or Iterators named it or iter - if you have two loops one after another, and in the second loop you type i when you should have said i2 (or "iter" when you should have typed "iter2", you get a bug. Such bugs are easily avoided (or promptly detected) if each loop variable is only defined within the loop it's needed for.

I suggest reading Effective Java, Item 29: Minimize the scope of local variables. Also the rest of the book is strongly recommended, for other reasons.


So you think it is ok? You do realize that just because something does not adversely effect a program, doesn't mean it should be done. The second code example is bad programming practice, period.
[ March 28, 2006: Message edited by: Rusty Shackleford ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by Rusty Shackleford:
The second code example is bad programming practice, period.
You said that before, but you didn't say why. So how about saying why this time?
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Because doing something that is not necessary is never something you should do. Regardless of whether it is legal or has no adverse effects on a program.

Flip it around, what does redeclaring a variable inside a loop buy you? If the answer is nothing(and it is), then there is no reason to do it. Do that in an academic assignment or real world code at your job and see what it buys you.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Declaring a variable close to where it's used helps readability, prevents scope errors, and most important to me helps the refactor tool in Eclipse. If the loop grows big enough to be in a separate method or class, you or the tool can cut/paste variables declared inside the loop with no effort.

It took me a long time to get to this style. I started in COBOL where all fields are declared at the top of the program, then a 370 Assembler class where they taught declare all the data at the end of the program and then Pascal where the style I took up was to very neatly declare variables at the top of each function.

BTW: COBOL folks are the champs at reusing variables in memory, files and databases. And it's absolutely disastrous. Never fall to the temptation.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Hmmm, I suppose I should've moved this to the Performance forum in the first place. Because even though this has little or no bearing in performance, we're certainly talking about performance a lot. And Beginner doesn't seem a good fit. So, off we go...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Performance!!