| Author |
Static variables Performance Issue
|
akmal jah
Ranch Hand
Joined: Feb 18, 2002
Posts: 31
|
|
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
Joined: Jun 06, 2002
Posts: 1056
|
|
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.
|
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
|
 |
akmal jah
Ranch Hand
Joined: Feb 18, 2002
Posts: 31
|
|
|
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
Joined: Jul 11, 2001
Posts: 14112
|
|
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
|
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
|
 |
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
|
|
|
Creating a copy of it just adds extra overhead (and confusion) and no performance gain.
|
"JavaRanch, where the deer and the Certified play" - David O'Meara
|
 |
 |
|
|
subject: Static variables Performance Issue
|
|
|