Consider the following snippet, where you can get a float (or the corresponding type) from the MyType instance argument:
void myMethod(MyType floatArgument) {
float myFloat = (float)MyType.Value;
if (myFloat < 100) {
//not meaningfull code
}
}
If the method is being continiusly called in a listener, or an Update of a physics or graphics engine, is it strictly better regarding efficiency to make it a class variable like this?? :
private float myFloat
void myMethod(MyType floatArgument) {
myFloat = (float)MyType.Value;
if (myFloat < 100) {
//not meaningfull code
}
}
The variable is used only in local scope, so AFAIK it would be created in the stack every time. So it would be faster to make a class variable from it, so that even the memory for that variable would be on the heap and the memory consumption would be higher, the code would run faster.
I know that for a float might not be a big deal, but take it as it if would be lots of variables or an instance of a large data class.
Is this reasoning correct?