By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

reference VS. pointer

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"calvin" <ca******@operamail.com> wrote in message news:38**************************@posting.google.c om...
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.
Jul 22 '05 #2

P: n/a
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f**********************@news.newshosting.com ...

"calvin" <ca******@operamail.com> wrote in message news:38**************************@posting.google.c om...
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

Jul 22 '05 #3

P: n/a
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.

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.