Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is accessing c++ member class through "this->member" faster/slower than implicit call to "member"

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 ?

like image 222
moustik Avatar asked Oct 26 '11 08:10

moustik


2 Answers

No, the call is exactly the same in both cases.

like image 166
zennehoy Avatar answered Sep 29 '22 13:09

zennehoy


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.

% diff -s with-this.cpp without-this.cpp
7c7
<         this->x = 5;
---
>         x = 5;

% g++ -c with-this.cpp without-this.cpp  

% diff -s with-this.o without-this.o   
Files with-this.o and without-this.o are identical
like image 33
Johnsyweb Avatar answered Sep 29 '22 12:09

Johnsyweb