• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Performance!!

 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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.
 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Sheriff
Posts: 28346
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic