After some searching on our friend google, I could not get a clear view on the following point.
I'm used to call class members with this->
. Even if not needed, I find it more explicit as it helps when maintaining some heavy piece of algorithm with loads of vars.
As I'm working on a supposed-to-be-optimised algorithm, I was wondering whether using this->
would alter runtime performance or not.
Does it ?
No, the call is exactly the same in both cases.
Answer has been given by zennehoy and here's assembly code (generated by Microsoft C++ compiler) for a simple test class:
Disassembly Window in Visual Studio shows that assembly code is the same for both functions:
And the code in the main:
Microsoft compiler uses
__thiscall
calling convention by default for class member calls andthis
pointer is passed via ECX register.There are several layers involved in the compilation of a language.
The difference between accessing
member
asmember
,this->member
,MyClass::member
etc... is a syntactic difference.More precisely, it's a matter of name lookup, and how the front-end of the compiler will "find" the exact element you are referring to. Therefore, you might speed up compilation by being more precise... though it will be unnoticeable (there are much more time-consuming tasks involved in C++, like opening all those includes).
Since (in this case) you are referring to the same element, it should not matter.
Now, an interesting parallel can be done with interpreted languages. In an interpreted language, the name lookup will be delayed to the moment where the line (or function) is called. Therefore, it could have an impact at runtime (though once again, probably not really noticeable).
It doesn't make any difference. Here's a demonstration with GCC. The source is simple class, but I've restricted this post to the difference for clarity.