# Pointers - Fast for Math Functions?

• 11-09-2009
MPQC
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?
• 11-09-2009
bithub
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.
• 11-09-2009
cyberfish
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).
• 11-10-2009
iMalc
Quote:

Originally Posted by MPQC
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".
• 11-10-2009
anon
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.
• 11-10-2009
MPQC
Alright. Thanks. :]