![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 7
| Pointers - Fast for Math Functions? 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;
}
|
| MPQC is offline | |
| | #2 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 3,020
| 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. |
| bithub is offline | |
| | #3 |
| Registered User Join Date: Dec 2006 Location: Canada
Posts: 2,008
| 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). |
| cyberfish is offline | |
| | #4 |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,732
| 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 |
| iMalc is offline | |
| | #5 | |
| The larch Join Date: May 2006
Posts: 3,222
| 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.
__________________ I might be wrong. Quote:
Last edited by anon; 11-10-2009 at 09:40 AM. | |
| anon is offline | |
| | #6 |
| Registered User Join Date: Oct 2009
Posts: 7
| Alright. Thanks. :] |
| MPQC is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| HELP WITH FUNCTIONS and POINTERS!!! | cjohnson412 | C++ Programming | 4 | 08-11-2008 10:48 PM |
| Passing Structure Pointers to Functions | samus250 | C Programming | 15 | 03-20-2008 03:13 PM |
| pointers to functions | null | C Programming | 3 | 09-30-2001 12:48 PM |
| Pointers, arrays , functions | sballew | C Programming | 19 | 09-16-2001 11:12 PM |