In a (C++) class I took recently the teacher mentioned that using the ->
operator was a tiny bit slower than using dot notation and dereferencing your pointer manually (e.g. (*ptr)
).
- Is this true, and, if so, why?
- Does this apply to C as well?
In a (C++) class I took recently the teacher mentioned that using the ->
operator was a tiny bit slower than using dot notation and dereferencing your pointer manually (e.g. (*ptr)
).
The ->
operator is neither slower or faster than .
operator. The fact is that dereferencing something is slower than just access to a memory location, because there is one more indirection. And this is a fact of life, either in C and C++ and any other language.
In C++, you have also references, so you can dereference something using the .
too! So the problem here is not arrow-vs-dot, the problem is if the compiler can go straight to a value or if it must search for its address before.