C++ performance of accessing member variables versus local variables

后端 未结 11 2021
别那么骄傲
别那么骄傲 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: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.

提交回复
热议问题