If you have a reference to something cached…
eg: var currentData : Data = myClassl.list.SelectedItem.data;
… is it worth caching sub-info like:
var myInt : int = currentData.subData;
or is it just as fast to refer to currentData.subData directly?
(pardon my random examples:)
function SomeFunction () {
var currentStage : Stage = stageData.someClass.data;
var currentStageProgress = currentStage.progress;
var currentStageID = currentStage.id;
DoSomethingMeaningful (currentStageProgress, currentStageID);
}
or
function SomeFunction () {
var currentStage : Stage = stageData.someClass.data;
DoSomethingMeaningful (currentStage.progress, currentStage.id);
}
This is keeping in mind limited platforms like the iPhone, etc., where even a minor shave off the overall load it worth a line of code.
[edit]
And what if it’s reused?
function SomeFunction () {
var currentStage : Stage = stageData.someClass.data;
currentStage.progress = currentStage.progress + 1;
if (currentStage.progress > someNumber) {
DoOneThing (currentStage.progress);
currentStage.progress + 1;
DoAnotherThing (currentStage.progress);
} else if (currentStage.progress < someothernumber) {
DoOneThingDifferent (currentStage.progress);
currentStage.progress - 1;
DoAnotherThingStrange (currentStage.progress * -1);
}
DoSomethingMeaningful (currentStage.progress, currentStage.id);
}
short answer is, is there a lot of processing attached?
myclass.mysomething().data
if mysomething() contains a huge amount of processing like a load of table lookups and stuff to return the value then you’ll probably want to cache it.
If it just contains
mysomething() { return data; }
then there’s no speed advantage for caching.
“where even a minor shave off the overall load it worth a line of code.” isn’t true.
When it comes to optimization, good program design provides the best optimization instead of worrying about the little things. After all it’s the game engine itself that most of the processing goes on. For example, I had a huge performance hit that took days to figure out. Turned out I was constantly moving one of my main prefabs and had forgot to add a kinematic rigidbody.
i agree with huw, if you just want to access some properties, you shouldn’t waste the extra memory you need for more variables. besides, usually the code tends to get more complicated with too many variables imo.
accessing properties doesn’t take much longer than a couple of nanoseconds, so even if you run through the loop a million times this won’t make much of a difference compared to accessing a variable directly.
Cool.
I guess that’s what I wanted to know. Like so many people here, I’m learning on the job (as it were) and have no formal instruction when it comes to coding, and it’s nice to have some confirmation either way.
Thanks for the info!