is there a Performance difference between this:
int test;
void Update()
{
test +=2;
}
and this:
void Update()
{
int test;
test +=2;
}
—
int main()
{
while(true)
Update();
}
I ask because the second Code is better to read (you don’t need to declare it at Class headers), so i would use it if the performance is not lower.
It is highly unlikely that there is a performance difference between the two snippets only profiling your code can tell reliably but there is a important functional difference tha you should consider here.
If your
testvariable is needed only inside the functionupdate()then you must declare it inside the function. That way the variable has a limited scope inside the function.The lifetime of such a local variable is limited to the scope where it resides.i.e. Within the function body, till the closing brace}.If at all you want your
testvariable to maintain state across function calls then it can be a local static variable declared inside the function.Declaring
testoutside the function makes it an global variable. And it can be accessible in any function in the same file.Also being a global variable it lifetime extends till end of program.