reference VS. pointer

Discussion in 'C++' started by calvin, Dec 5, 2003.

  1. calvin

    calvin Guest

    I originally thought that using a reference to access an object is
    more efficient than using a pointer.

    Given an simple example like this,

    int i = 12;
    int& r = i;
    int* p = &i;

    the memory lay-out would look like,

    |................ |
    address of i and r |12 |
    |.................|
    |.................|
    address of p |address of i |
    |.................|

    In order to access the value of i (12) through pointer p, you would have to
    1. get the value of variable p;
    2. use the value of p as the address of a memory chunk;
    3. get the value of that memory address.

    This would take at least two cpu clock ticks.

    If reference r is used to access the value of i (12), you would only need to
    1. get the value of variable r, since r is the same as i.

    Surprisingly, when I wrote a short program to verify this, I got
    totally reversed result. The pointer is faster than reference!
    My test is based on GCC 3.2 on a W2K system.

    Replies are appreciated.

    -calvin
    calvin, Dec 5, 2003
    #1
    1. Advertising

  2. calvin

    Ron Natalie Guest

    "calvin" <> wrote in message news:...

    > int i = 12;
    > int& r = i;
    > int* p = &i;
    >
    > the memory lay-out would look like,


    Not necessarily.


    >
    > Surprisingly, when I wrote a short program to verify this, I got
    > totally reversed result. The pointer is faster than reference!
    > My test is based on GCC 3.2 on a W2K system.
    >
    > Replies are appreciated.


    Lets see your test program. Also did you turn on the optimizer?
    There is no point in benchmarking most compiler performances in the
    unoptimized mode.
    Ron Natalie, Dec 5, 2003
    #2
    1. Advertising

  3. "Ron Natalie" <> wrote in message
    news:3fd0dcc8$0$7506$...
    >
    > "calvin" <> wrote in message

    news:...
    >
    > > int i = 12;
    > > int& r = i;
    > > int* p = &i;
    > >
    > > the memory lay-out would look like,

    >
    > Not necessarily.
    >
    > >
    > > Surprisingly, when I wrote a short program to verify this, I got
    > > totally reversed result. The pointer is faster than reference!
    > > My test is based on GCC 3.2 on a W2K system.
    > >
    > > Replies are appreciated.

    >
    > Lets see your test program. Also did you turn on the optimizer?
    > There is no point in benchmarking most compiler performances in the
    > unoptimized mode.


    Also when the optimizer is on, there is big chance it doesn't matter whether
    you use pointers or references. A good exercise is to study the assembly
    output of an optimizing compiler; one of the most important lessons one will
    learn is that that those nano optimizations are futile; in many cases they
    have no effect whatsoever.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Dec 5, 2003
    #3
  4. Peter van Merkerk wrote:

    >
    >
    > Also when the optimizer is on, there is big chance it doesn't matter whether
    > you use pointers or references. A good exercise is to study the assembly
    > output of an optimizing compiler; one of the most important lessons one will
    > learn is that that those nano optimizations are futile; in many cases they
    > have no effect whatsoever.
    >


    right ...

    But there are a number of things you can do that will preclude the
    compiler from making optimizations - which in and of itself is not a bad
    thing.

    Many compilers today do an excellent job of inlining code and in the
    long run, this results in smaller and faster code if the functions are
    small enough. However, the compiler can't do much with inlining across
    a virtual function call.

    So to get back at your example, if you want to see the difference,
    create a virtual member function, and test calling it with references or
    pointers. If you see a difference, I'll be surprised.
    Gianni Mariani, Dec 6, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sam pal
    Replies:
    3
    Views:
    514
    E. Robert Tisdale
    Jul 16, 2003
  2. Replies:
    10
    Views:
    662
    Chris Torek
    Feb 4, 2005
  3. Replies:
    4
    Views:
    374
    Andre Kostur
    Dec 24, 2007
  4. mathieu
    Replies:
    8
    Views:
    495
    Juha Nieminen
    Aug 31, 2008
  5. A
    Replies:
    7
    Views:
    620
Loading...

Share This Page