C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-09-2009, 05:54 PM   #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?
MPQC is offline   Reply With Quote
Old 11-09-2009, 06:13 PM   #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   Reply With Quote
Old 11-09-2009, 10:52 PM   #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   Reply With Quote
Old 11-10-2009, 02:49 AM   #4
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,732
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
iMalc is offline   Reply With Quote
Old 11-10-2009, 09:35 AM   #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:
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).

Last edited by anon; 11-10-2009 at 09:40 AM.
anon is offline   Reply With Quote
Old 11-10-2009, 03:00 PM   #6
Registered User
 
Join Date: Oct 2009
Posts: 7
Alright. Thanks. :]
MPQC is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:48 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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