• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static variables Performance Issue

 
akmal jah
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this class A and it has a boolean variable isValid. I am using this as
if(A.isValid) in most of the classes. but in classes where I use this several times ( more than 50 times ), I am making a local copy of it like below, like in
class B : localIsValid = A.isValid;
what is a best solution. making a local copy in B or using directly from A . details below..
FYI.. I am writing only relevant part of my class
public class A {
...
public static boolean isValid = false;
...
}
public class B {

...
// Type 1 implementation
public localIsValid = A.isValid; //1
if(localisvalid){
// do nothing
}
// Type 2
if(A.isValid){
// do nothing
}
...
}
what I did was , made small app out of this and did some time testing but it doesnt tell much.
What I want is some kind of thoery behind this and how it works internally and how they differ internally. ( and what type ( 1 / 2 ) is a good implementation )
apreciate it . thanks
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're trying to optimize prematurely.
If your static variable can change, you'll have to keep the two copies in sync. If it can't change, you should declare it to be final.
 
akmal jah
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, once loaded from the property file, the variable value does not change , so I dont need to keep that particular variable in sync, can you please tell me which implementation is better and why , will it help making that variable final optimize my code ? thanks for your help
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think you need to performance-optimize it?
In most projects, maintenance is a much more critical issue than performance. And even if performance becomes an issue, you are very likely to optimize the wrong thing until you searched the real bottlenecks using a profiler.
See http://c2.com/cgi/wiki?RulesOfOptimization
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating a copy of it just adds extra overhead (and confusion) and no performance gain.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic