Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local variables vs instance variables

I've been doing a lot of research on C# optimization for a game that I'm building with XNA, and I still don't quite understand whether local variables are instance variables give better performance when constantly being updated and used.

According to http://www.dotnetperls.com/optimization , you should avoid parameters and local variables, meaning instance variables are the best option in terms of performance.

But a while ago, I read on another StackOverflow post (I can't seem to find where it was) that local variables are stored in a part of memory that is far quicker to access, and that every time an instance variable is set, the previous value has to be erased as a tedious extra step before a new value can be assigned.

I know that design-wise, it might break encapsulation to use instance variables in that kind of situation, but I'm strictly curious about performance. Currently in my game, I pass around local variables to 3 out of 7 methods in a class, but I could easily promote the variables to instance variables and be able to entirely avoid parameter passing and local variables.

So which would be better?

like image 332
Blake Thiessen Avatar asked Jul 27 '12 08:07

Blake Thiessen


People also ask

What is the difference between instance variable and global variable?

gx is a global variable accessible anywhere in the module, ix is an instance variable that could have a unique value for each instance of the object. When referenced inside of the object definition you would refer to ix with the prefix self , and when outside the object with a prefix of the object reference.

What's the difference between instance and variable?

They are tied to a particular object instance of the class, therefore, the contents of an instance variable are totally independent of one object instance to others. Class Variable: It is basically a static variable that can be declared anywhere at class level with static.


1 Answers

Are your variables reference (class, or string) or value (struct) types?

For reference types there's no meaningful difference between passing them as a method argument and holding them on an object instance. In the first case when entering the function the argument will (for functions with a small argument count) end up in a register. In the second case the reference exists as an offset of the data pointed to in memory by 'this'. Either scenario is a quick grab of a memory address and then fetching the associated data out of memory (this is the expensive part).

For value types the above is true for certain types (integers or floats that can fit in your CPU's registers). For those specific things it's probably a little cheaper to pass-by-value vs. extracting them off 'this'. For other value types (DateTime or structs you might make yourself or any struct with multiple members) when the data is going to be too large to pass in through a register so this no longer matters.

It's pretty unlikely, though, that any of this matters for the performance of your application (even a game). Most common .NET performance problems (that are not simply inefficient algorithms) are going to, in some form, come from garbage generation. This can manifest itself through accidental boxing, poor use of string building, or poor object lifetime management (your objects have lifespans that are neither very short nor very long/permanent).

like image 131
Chip Locke Avatar answered Sep 28 '22 21:09

Chip Locke