Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ performance of accessing member variables versus local variables

People also ask

Which variable is faster in C?

Registers are faster than memory to access, so the variables which are most frequently used in a C program can be put in registers using register keyword.

What are the difference between local variables and member variables?

A local variable is the variable you declare in a function. Its lifespan is on that Function only. A member variable is the variable you declare in a class definition. Its lifespan is inside that class only.It is Global Variable.It can be access by any function inside that same class.

Are local variables more efficient?

Local variables can be faster because the optimizer can store them in registers. In C++ in general, you can't make a statement about the performance difference.

What is the difference between local variable and data member in C++?

Data member belongs to class/objects. Local variables, not necessary (it can belong to a function). 2. A data member can be accesed outside of the object but a local variables not.


Executive summary: In virtually all scenarios, it doesn't matter, but there is a slight advantage for local variables.

Warning: You are micro-optimizing. You will end up spending hours trying to understand code that is supposed to win a nanosecond.

Warning: In your scenario, performance shouldn't be the question, but the role of the variables - are they temporary, or state of thisClass?

Warning: First, second and last rule of optimization: measure!


First of all, look at the typical assembly generated for x86 (your platform may vary):

// stack variable: load into eax
mov eax, [esp+10]

// member variable: load into eax
mov ecx, [adress of object]
mov eax, [ecx+4]

Once the address of the object is loaded, int a register, the instructions are identical. Loading the object address can usually be paired with an earlier instruction and doesn't hit execution time.

But this means the ecx register isn't available for other optimizations. However, modern CPUs do some intense trickery to make that less of an issue.

Also, when accessing many objects this may cost you extra. However, this is less than one cycle average, and there are often more opprtunities for pairing instructions.

Memory locality: here's a chance for the stack to win big time. Top of stack is virtually always in the L1 cache, so the load takes one cycle. The object is more likely to be pushed back to L2 cache (rule of thumb, 10 cycles) or main memory (100 cycles).

However, you pay this only for the first access. if all you have is a single access, the 10 or 100 cycles are unnoticable. if you have thousands of accesses, the object data will be in L1 cache, too.

In summary, the gain is so small that it virtually never makes sense to copy member variables into locals to achieve better performance.


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.


This should be your compilers problem. Instead, optimize for maintainability: If the information is only ever used locally, store it in local (automatic) variables. I hate reading classes littered with member variables that don't actually tell me anything about the class itself, but only some details about how a bunch of methods work together :(

In fact, I would be surprised if local variables aren't faster anyway - they are bound to be in cache, since they are close to the rest of the functions data (call frame) and an objects pointer might be somewhere totally else - but I am just guessing here.


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.

  • Pointer to Object + offset
  • Pointer to Stack Frame + 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.