reference VS. pointer

C

calvin

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
 
R

Ron Natalie

calvin said:
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.
 
P

Peter van Merkerk

Ron Natalie said:
Not necessarily.


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.
 
G

Gianni Mariani

Peter said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top