Thread: Dereferencing pointer vs local variable performance ( Another noob question :) )

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    73

    Dereferencing pointer vs local variable performance ( Another noob question :) )

    Hi,
    This is probably been asked here before...but:

    what is faster ?
    this:
    Code:
    int main()
    {
        int n[] = {1,2,3,4,5,6,7,8,9,10};
        int *p = n;
        int i;
        
        for(i = 0; i < 100; i++){
            printf("\n%d",*(p+4)*i);
        }
        return 0;
    }
    or this:
    Code:
    int main()
    {
        int n[] = {1,2,3,4,5,6,7,8,9,10};
        int *p = n;
        int i;
        int j = *(p+4);
        for(i = 0; i < 100; i++){
            printf("\n%d",j*i);
        }
        return 0;
    }
    now let's say that i have a function that needs to dereference a pointer millions of times , in order to get it's value.
    would it be better to just dereference it once and store the value into a local variable and just use that variable ? will it be faster ?

    but specific to my function, using the pointer dereference style would result in a cleaner and a little simpler code, and also save me a variable (like int j in the above example).

    Thx.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Why would you want to do this in either of the ways that you show? Both are highly convoluted, and pointless. Why not just use array syntax? You're worried about things that don't matter. In some cases, attempts by the programmer to write "faster" code will result in the compiler having a harder time with the source code, and generating slower machine code.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    73
    To answer you question , I just gave an example to show what I mean (I was lazy). In my actual program (a game engine) I hold a global array of pointers, and I use them to access board indexes. (each pointer points to a line of 5 squares) this is for the win checking and the evaluation function.

    so as you can see, I am going to dereference pointers like crazy and back to my question... is it faster (and preferable) to keep the pointer value inside a local variable and use it, or just dereference the pointer every time ? My logic tells me that local variable will be faster becasue dereferencing a pointer will probably take longer, but i am not sure.

    By the way, from you answer, accessing array directly is faster than dereferencing a pointer ? I thought that the speed should be the same.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by patishi View Post
    is it faster (and preferable) to keep the pointer value inside a local variable and use it, or just dereference the pointer every time ? My logic tells me that local variable will be faster becasue dereferencing a pointer will probably take longer, but i am not sure.
    Either way, you're accessing memory through a pointer. Where that pointer actually resides in memory is largely irrelevant.

    Quote Originally Posted by patishi View Post
    By the way, from you answer, accessing array directly is faster than dereferencing a pointer ? I thought that the speed should be the same.
    I didn't say that array syntax is faster. I said that the convoluted ways you showed might be slower.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    73
    so doing:
    Code:
    int i = 4;
    int j;
    for(j = 0; j < 1000000; j++){
         i += j*i;
    }
    is the same as:
    Code:
    int i = 4;
    int *p = &i;
    int j;
    for(j = 0; j < 1000000; j++){
        *p += j * (*p);
    }
    ??

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Effectively, yes. Depending on the way the compiler generates machine code, and depending on the architecture, it may be a clock cycle or two slower to dereference a pointer, but when you compile with full optimization, chances are there will be no difference at all.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Aug 2013
    Posts
    73
    Thx a lot

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Your example really doesn't have anything to do with your use case. You say you have a set of global pointers to some shared data. The loop example is nothing like that (I wouldn't be surprised if the compiler unrolled and const-propagated it even).

    A set of global pointers is really the worst possible case for the optimizer, as they potentially all alias each other and nothing can be cached in registers. I'll go against the group and speculate that manually caching the values MIGHT make your code faster.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > what is faster ?
    Alert Alert!! - Danger Will Robinson - Premature Optimisation Disease outbreak in progress!

    Forget about trying to micro-optimise your code and focus on getting some code which is actually functionally correct to begin with.

    Then (and only then) do some profiling with real data to find out where the real hot-spots are.

    Leave the micro-optimisations to the compiler, and focus on the big picture aspects, such as choosing the right algorithms and data structures for the problem at hand. Getting these right or wrong will have far more impact on your program than worrying about whether p[i] or *(p+i) is quicker.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-11-2011, 10:50 AM
  2. Adding a local Variable to a Vector Question
    By cpsmusic in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2011, 08:34 AM
  3. Local variable Question
    By WDT in forum C# Programming
    Replies: 2
    Last Post: 04-02-2009, 05:39 PM
  4. uninitialized local variable question
    By raist in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2006, 03:00 PM
  5. Replies: 20
    Last Post: 11-12-2005, 03:10 PM