Search code examples
cperformancememorymemory-access

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 ?

Solution

  • 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.