Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance difference when accessing using pointer and double pointer

  1. Is there any performance difference when we access a memory location by using a pointer and double pointer?
  2. If so, which one is faster ?
like image 289
Chinna Avatar asked Feb 13 '14 11:02

Chinna


2 Answers

There is no simple answer it, as the answer might depend in the actual machine. If I remember correctly some legacy machines (such as PDP11) offered a 'double pointer' access in a single instruction.

However, this is not the situation today. accessing memory is not as simple as it looks and requires a lot of work, due to virtual memory. For this reason - my guess is that double reference should in fact be slower on most modern machines - more work has to be done to translate two addresses from virtual addresses to physical addresses and retrieving them - but that's just educated guess.
Note however, that the compiler might optimize 'redundant' accesses for you already.

For my best knowledge however, there is no machine that has faster 'double access' than 'single access', so we can say that single access is not worse than double access.

As a side note, I believe in real life programs, the difference is neglectable (comparing to anything else done in the program), and unless done in a very performance sensitive loop - just do whatever is more readable. Also, the compiler might optimize it for you already if it can.

like image 139
amit Avatar answered Sep 27 '22 23:09

amit


Assuming you are talking about something like

int a = 10;
int *aptr = &a;
int **aptrptr = &aptr;

Then the cost of

*aptr = 20;

Is one dereference. The address pointed to by aptr must first be retrieved and then the address can be stored to.

The cost of

**aptrptr = 30;

Is two dereferences. The address pointed to by aptrptr must first be retrieved. Then the addess stored in that address must be retrieved. Then this address can be stored to.

Is this what you were asking?

Therefore, to conclude using a single pointer is faster if that suits your needs.

Note, that if you access a pointer or double pointer in a loop, for example,

while(some condition)
    *aptr = something;

or

while(some condition)
    **aptrptr = something;

The compiler will likely optimize so that the dereferencing is only done once at the start of the loop, so the cost is only 1 extra address fetch rather than N, where N is the numnber of times the loop executes.

EDIT: (1) As Amit correctly points out the "how" of pointer access is not explicitly a C thing... it does depend on the underlying architecture. If your machine supports a double dereference as a single instruction then there might not be a big difference. He is using the index deferred addressing mode of the PDP11 as an example. You might find out that such an instruction still chews up more cycles... consult the hardware documentation and look at the optimization that your C compiler is able to apply for your specific architecture.

The PDP11 architecture is circa the 1970s. As far as I know (if someone knows are modern architecture that can do this pleas post!), most RISC architectures and don't have such a double dereference and will probably need to do two fetches as far as I am aware.

Therefore, to conclude using a single pointer is probably faster generally, but with the caveat that specific architectures may handle this better than others and compiler optimizations, as I discussed, could make the difference negligible... to be sure you just have to profile your code and read up about your architecture :)

like image 31
Jimbo Avatar answered Sep 28 '22 01:09

Jimbo