Is it more efficient for a class to access member variables or local variables? For example, suppose you have a (callback) method whose sole responsibility is to receive dat
Also, there's a third option: static locals. These don't get re-allocated every time the function is called (in fact, they get preserved across calls) but they don't pollute the class with excessive member variables.
A few points that have not been mentioned explicitly by others:
You are potentially invoking assignment operators in your code. e.g varC = msg.getString();
You have some wasted cycles every time the function frame is setup. You are creating variables, default constructor called, then invoke the assignment operator to get the RHS value into the locals.
Declare the locals to be const-refs and, of course, initialize them.
Member variables might be on the heap(if your object was allocated there) and hence suffer from non-locality.
Even a few cycles saved is good - why waste computation time at all, if you could avoid it.
In my oppinion, it should not impact performance, because:
Therefore, there is not much of a difference.
However, You should avoid copying the string ;)
Silly question.
It all depends on the compiler and what it does for optimization.
Even if it did work what have you gained? Way to obfuscate your code?
Variable access is usually done via a pointer and and offset.
Also don't forget to add in the cost of moving the variables to local storage and then copying the results back. All of which could be meaning less as the compiler may be smart enough to optimize most of it away anyway.
I'd prefer the local variables on general principles, because they minimize evil mutable state in your program. As for performance, your profiler will tell you all you need to know. Locals should be faster for ints and perhaps other builtins, because they can be put in registers.
When in doubt, benchmark and see for yourself. And make sure it makes a difference first - hundreds of times a second isn't a huge burden on a modern processor.
That said, I don't think there will be any difference. Both will be constant offsets from a pointer, the locals will be from the stack pointer and the members will be from the "this" pointer.