I am trying to figure out how many clock cycles or total instructions it takes to access a pointer in C. I dont think I know how to figure out for example, p->x = d->a + f->b
i would assume two loads per pointer, just guessing that there would be a load for the pointer, and a load for the value. So in this operations, the pointer resolution would be a much larger factor than the actual addition, as far as trying to speed this code up, right?
This may depend on the compiler and architecture implemented, but am I on the right track?
I have seen some code where each value used in say, 3 additions, came from a
f2->sum = p1->p2->p3->x + p1->p2->p3->a + p1->p2->p3->m
type of structure, and I am trying to define how bad this is
This depends on the architecture at hand.
Some architectures can reference/dereference memory for an instruction without first loading it into a register, others don’t. Some architectures don’t have the notion of instructions that compute the offsets for you to dereference and will make you load the memory address, add your offset to it, and then allow you to dereference the memory location. I’m sure there are more variances chip-to-chip.
Once you get past these, each instruction takes varying amount of time depending on the architecture as well. To be honest though, it’s an overhead that is very, very minimal.
For your immediate question of dereferencing a chain of items, the slowness will come in the fact that there is likely a poor locality of reference the farther you go in a dereferencing chain. This means more cache misses, which means more hits to main memory (or disk!) to get the data. Main memory is very slow compared to the CPU.