- Home /
function is faster when accessing function variables not global ones?
i have a function FindThings() that compares lots of Vector3's found with findWithTag to one global var PlayerLocation:Vector3 from the top of the script
it seems that if FindThings() calls PlayerLocation multiple times from the global variables, it goes slower than if PlayerLocation is sent to the function as a function variable:
FindThings(PlayerLocation);
{
Do various PlayerLocation comparisons;
}
is faster than
var PlayerLocation:Vector3;
FindThings()
{
Do various PlayerLocation comparisons;
}
EDIT: this function is going very slowly, it was even slower when location was a global variable. i have another version of the same function that goes 100 times faster. it's odd.
function delAdjoining(location: Vector3)//Dell all walls surrounding a space if revisited prior to rebuilding
{
for (var j:int = 0; j < neighbours.length; j ++)
{
if ( neighbours[j] == location )
{
var dwalls = GameObject.FindGameObjectsWithTag ("wall");
for (var dwall in dwalls)
{
if(dwall.transform.position == (location + stp1*.5*sizeMult)){Destroy(dwall);}
if(dwall.transform.position == (location + stp2*.5*sizeMult)){Destroy(dwall);}
if(dwall.transform.position == (location + stp3*.5*sizeMult)){Destroy(dwall);}
if(dwall.transform.position == (location + stp4*.5*sizeMult)){Destroy(dwall);}
if(dwall.transform.position == (location + stp5*.5*sizeMult)){Destroy(dwall);}
if(dwall.transform.position == (location + stp6*.5*sizeMult)){Destroy(dwall);}
print("erased adjoining");
}
break;
}
}
}
Answer by Eric5h5 · Oct 09, 2012 at 01:22 PM
Yes, although the difference is very small. You would generally prefer to keep variables local primarily because of better script organization; the speed benefit is just a bonus since it will not be measurable in most cases.
I have a function that takes about 1-2 seconds to process on an old laptop, and i changed it to a function variable and it seemed to take about 1/2 a second to process.
I also have another version of the same function that is written almost exactly the same, but takes about 1/100th of a second. i updated my original question with the slow code
You'd have to loop millions of times to be able to tell the difference in the profiler, never $$anonymous$$d a stopwatch, so I expect you have some other difference as well. One thing you can do to speed up the function (which will make far more of a difference) is to declare a variable, "var dwallTransform = dwall.transform;" and use that ins$$anonymous$$d of dwall.transform, and use "else if" ins$$anonymous$$d of "if", so it will skip evaluating those conditions where it doesn't need to.
Answer by BPPHarv · Oct 09, 2012 at 07:26 PM
That really slow code you posted as your edit is slow because you're doing GameObject.FindGameObjectsWithTag ("wall"); repeatedly.
FindGameObjectWithTag is slow. To get a true sense of the speed gain cache the result of FindGameObjectWithTag in an array.
Answer by hvilela · Oct 09, 2012 at 06:49 PM
Move your GameObject.FindGameObjectsWithTag outside the loop, cause it's slowly.
function delAdjoining(location: Vector3)//Dell all walls surrounding a space if revisited prior to rebuilding
{
var dwalls = GameObject.FindGameObjectsWithTag ("wall");
for (var j:int = 0; j < neighbours.length; j ++)
{
if ( neighbours[j] == location )
{
for (var dwall in dwalls)
{
if(dwall.transform.position == (location + stp1*.5*sizeMult)){Destroy(dwall);}
if(dwall.transform.position == (location + stp2*.5*sizeMult)){Destroy(dwall);}
if(dwall.transform.position == (location + stp3*.5*sizeMult)){Destroy(dwall);}
if(dwall.transform.position == (location + stp4*.5*sizeMult)){Destroy(dwall);}
if(dwall.transform.position == (location + stp5*.5*sizeMult)){Destroy(dwall);}
if(dwall.transform.position == (location + stp6*.5*sizeMult)){Destroy(dwall);}
print("erased adjoining");
}
break;
}
}
}
Your answer
Follow this Question
Related Questions
Function is not updating the variable 1 Answer
Accessing variables from seperate scripts 1 Answer
Custom Global Functions 1 Answer
How can you hide variables within a region in a c# script? 1 Answer
How i "import" variables? 2 Answers