C++ performance of accessing member variables versus local variables

后端 未结 11 1932
别那么骄傲
别那么骄傲 2020-12-04 22:07

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

相关标签:
11条回答
  • 2020-12-04 22:26

    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.

    0 讨论(0)
  • 2020-12-04 22:27

    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.

    0 讨论(0)
  • 2020-12-04 22:27

    In my oppinion, it should not impact performance, because:

    • In Your first example, the variables are accessed via a lookup on the stack, e.g. [ESP]+4 which means current end of stack plus four bytes.
    • In the second example, the variables are accessed via a lookup relative to this (remember, varB equals to this->varB). This is a similar machine instruction.

    Therefore, there is not much of a difference.

    However, You should avoid copying the string ;)

    0 讨论(0)
  • 2020-12-04 22:32

    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.

    0 讨论(0)
  • 2020-12-04 22:38

    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.

    0 讨论(0)
  • 2020-12-04 22:40

    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.

    0 讨论(0)
提交回复
热议问题