Pointers - Fast for Math Functions?

This is a discussion on Pointers - Fast for Math Functions? within the C++ Programming forums, part of the General Programming Boards category; Quick question here, or at least hopefully so. I was just going through the tutorials ag, and writing myself simple ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    Pointers - Fast for Math Functions?

    Quick question here, or at least hopefully so. I was just going through the tutorials ag, and writing myself simple programs for examples for the future. So I was just thinking a bit, and wrote up this program:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
        int *pointer;
        int x;
        int answer;
    
        pointer = &x;
    
        cout<<"Insert a whole number: ";
    
        cin>> x;                                                               // Makes x = Number you said
    
        cin.ignore();
    
        cout<< "The number you chose is: ";
        cout<< *pointer;                                                 // Uses the pointer function, which points towards "x"
        cout<< "\nThis number multiplied by 5 is: ";
        answer = (*pointer * 5);                                    // Multiples x by 5, but using pointer
        cout<< ""<< answer <<"";                                 // Shows answer
        cin.get();
    
        return 0;
    }
    And the code works nicely. It basically multiplies whatever the person said, by 5. Simple. The question is, is this any faster, then actually doing x * 5? I recognize that using pointers will "grab" the said value faster, as all it has to do is point towards it, rather then actually grabbing it. Is the above true while using the variable in other things, such as multiplication?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    No, if anything that will be slower than just doing x * 5. It is easy for the compiler to optimize x, and place it in a register. A smart compiler may still be able to perform this optimization with a pointer, but it would definitely not be as simple.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    And even without optimizations, the pointer version is still slower. What the CPU now has to do, is to load the pointer from the stack, dereference it (which points to another address on the stack), then multiply, instead of just load and multiply.

    In general, don't do this kind of pre-mature optimizations, because most of the time, it either won't matter anyways, breaks your code, or make it slower (like in this case), or make it slower by making it harder for compilers to optimize (like bithub pointed out). Just write the most straight forward code, and let the compiler (optimizer) do its job. Modern optimizers are VERY smart, and you can help them by making your code as simple as possible. Unless you have a few decades of programming experience and are intimately familiar with the low-level workings of hardware, chances are, you won't do a better job optimizing than a compiler. Humans are really no match for them nowadays at micro-optimizations.

    Even if you CAN optimize better than a compiler, don't do it to the whole program. That's a waste of time and makes your code unnecessarily complex. Profile (do a sample run of your program, and determine where most of the time is spent, with the help of a profiler) first to find the bottleneck, then just optimize that part by hand.

    What you should think about, though, is algorithmic optimization, because optimizers won't do that for you, and it's where the most performance gain is usually found. You can try optimizing bubble sort until the sun burns out, and it will still be no match for an unoptimized naive implementation of quick sort (which the optimizer can then help you optimize).

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by MPQC View Post
    I recognize that using pointers will "grab" the said value faster, as all it has to do is point towards it, rather then actually grabbing it.
    Sorry but your understanding of what really happens is wrong if that's what you "recognize".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Pointers add another level of indirection. It just means that you'll need two steps to "grab" a value, rather than one. First you look in one place and find a value which represents an address, then you'll go to that address and only then find the value you were looking for (only if you are lucky can the compiler optimize the program so that it becomes as good as directly looking up a value).

    Pointers and references do have a place in writing fast programs, in that they allow you to avoid copying large objects when you pass them to functions (if the cost of copying outweighs the cost of extra indirection).

    But really, pointers have other purposes, such as allowing you to access array elements and dynamically allocated memory, or to have multiple references to the same object.
    Last edited by anon; 11-10-2009 at 08:40 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Alright. Thanks. :]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP WITH FUNCTIONS and POINTERS!!!
    By cjohnson412 in forum C++ Programming
    Replies: 4
    Last Post: 08-11-2008, 10:48 PM
  2. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 03:13 PM
  3. pointers to functions
    By null in forum C Programming
    Replies: 3
    Last Post: 09-30-2001, 12:48 PM
  4. Pointers, arrays , functions
    By sballew in forum C Programming
    Replies: 19
    Last Post: 09-16-2001, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21