I would say the difference of cost isn't in the operators themselves, but in the cost of accessing the left-side object.
ptr->variable
should produce a similar asm output than
(*ptr).variable // yeah I am using a '.' because it's faster...
therefore your question is kind of nonsensical as it is.
I think I understand what you meant though, so I'll try to answer that instead.
The operators themselves are nearly costless. They only involve computing an address. This address is expressed as the address of the object plus an offset (likely fixed at compile time and therefore a constant in the program for each field). The real cost comes from actually fetching the bytes at this address.
Now, I don't understand why it would be more costly to use a ->
vs a .
since they effectively do the same thing, there can be a difference of access though in this case:
struct A { int x; };
void function(A& external)
{
A local;
external.x = local.x;
}
In this case, accessing external.x
is likely to be more costly because it necessitates accessing memory outside of the scope of the function and therefore the compiler cannot know in advance whether or not the memory will have already been fetched and put in the processor cache (or a register) etc...
On the other hand local.x
being local and stored on the stack (or a register), the compiler may optimize away the fetch part of the code and access local.x
directly.
But as you notice there is no difference between using ->
vs .
, the difference is in using a local variable (stored on the stack) vs using a pointer / reference to an externally supplied object, on which the compiler cannot make an assumption.
Finally, it's important to note that if the function was inlined, then the compiler could optimize its use at the caller's site and effectively use a slightly different implementation there, but don't try to inline everything anyway: first of all the compiler will probably ignore your hints, and if you really force it you may actually lose performance.