Is it better to make a local scope variable a class variable if it is being used in an update/listener to avoid it being created every time?

Viewed 318

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?

2 Answers

In my opinion, it's a design decision. But mostly, you can simply create a local scope variable. Why? That's per design, if you call your method very, very, VERY often and rapidly, THEN you can get in trouble with your memory. On the other Hand, when you move it into class scope, everytime you create your class you will adress memory for your variable which can be never used. So, it's up to you and your design. Even performance, if you have an object, there is nearly NO performance issue rather than reserving a bit of memory for holding it because your object is set as reference.

For the sake of cleaner code, i would recommend using local scope in those cases.

As Tinoz said its more of a design design, there is technically a difference in performance but its insignificant. Having it in class scope will use memory to store it even when the variable isn't in use, where as having it in the methods scope will use slightly more CPU time to create it each time the method is run.

You could try to measure the difference but in reality its such a small amount that unless your calling the function thousands of times a second or having thousands of instances, it won't make much difference. Even then, the difference may still be pretty marginal. As an example, having a int in the class scope in C# only takes 4 bytes or 0.000004 MB roughly, couldn't say how much execution time defining the same int variable would take though.

It's insignificant enough that having the variable local to the method for tidier code outweighs the performance cost either way, but it does come down to the design of your application.

Edit: The thought occurs this may matter more when defining new instances of larger objects in some large scale applications. This would likely have a greater affect on performance depending on what the object has stored. Its still unlikely to matter a huge amount unless you have huge objects store vast amounts of data in which case you should check the design of your application to see if such a object needs to store that much data.

In this event, it comes down to memory usage vs CPU time. As mentioned before, storing variables in class scope will use memory all the time to store the data. In method scope would still use the same amount of memory but only whilst the method runs and would be cleared after. It would take more CPU cycles though to complete as your defining the variables each time.

For the sake of argument, lets say the memory usage and CPU time used for this is a significant amount. It becomes a balancing game of speed versus memory. If your trying to optimise for systems with less memory or similar reasoning, then defining in method scope for all methods saves memory cost though would increase execution time. If you want the task to be completed quicker and memory usage is a lower priority, then defining in class scope for all variables will use more memory as your storing more data for longer, but would decrease the execution time required.

It's still unlikely to matter either way as in most cases we are talking minute amounts of time and memory, but in rare cases where it does matter it would be best to consider the design of your application and what your trying to optimise best for

Related