Reference vs. pointers

I've recently discovered how to use reference arguments in an actually useful manner, but was surprised when I realized a large difference in speed between it and the use of pointers.

I.e.
1
2
3
4
5
6
7
8
9
10
11
float& Vector::operator[](int pos)
{
	return v[pos];
}
void Vector::Add(Vector& v1, Vector& v2)
{
    v[0] = v1[0]+v2[0];
    v[1] = v1[1]+v2[1];
    v[2] = v1[2]+v2[2];
    v[3] = 1;
}

Takes about twice as long to run as
1
2
3
4
5
6
7
void Vector::Add(Vector* v1, Vector* v2)
{
	v[0] = v1->v[0]+v2->v[0];
	v[1] = v1->v[1]+v2->v[1];
	v[2] = v1->v[2]+v2->v[2];
	v[3] = 1;
}


I had a program which rendered an image with a dozen or so functions all running by reference. Noticing a large load-time (around 6 seconds), I went on a limb and rebuilt them all using pointers instead. Now the program runs at around 3.5 seconds.

Is this something to do with the differences (still unclear to me) between references and pointers? Is it my operator[] in the first example that is somehow inefficient (with its whopping one line of code)?

Obviously, this is a program that uses these functions a few hundred thousand times, so any performance boost is appreciated.
Is your operator[] inlined?

I'm betting you're spending all your time calling that function and returning.
Those aren't the same...the reference one accesses the operator[] of vector (which is a function call at runtime since it is not inline), whereas the pointer one accesses a member called v and then uses the operator[] on that (which is all done at compile time).

Anyway, make your vector:: operator inline, and make the int you are passing const. That should make them more equal.
As far as I know, the compiler makes an assessment and decides itself whether or not to inline a function at compile-time. I didn't think that needs to be made explicit.

Does it?

And, as a refresher, inlining just needs a
1
2
inline void Vector::Add()
/*...*/

Right?

(I use VC++ 2008 Express, should it matter)
Last edited on
the only thing that would cause a drop in performance would be if your [] operator isn't being inlined.

1) make sure the function is inlined
2) make sure the compiler is able to inline functions (build as Release, not as Debug)


EDIT: doh I was way too slow.
Last edited on
To give the compiler the best chance of inlining, you should declare and implement the function in the header file.
Otherwise, no, it is unlikely if not impossible (depending upon code structure) that the compiler will silently
inline it for you.
1) It might be. I know in VC++ there are settings (optimization settings I believe) that tell the compiler to inline or not. If you are compiling in debug, there is no inlining done.

2) Yes, and put the function body in the header file (the same one you declaring it inline in).
Ah, Release!

That's it, probably. I'm spending most of my time debugging (for performance! How ironic), so I'm keeping it all in Debug build. That's probably why it's not inlining the operator-call. Does it inline in debug mode if you make it explicit, though?
Probably not. Otherwise it would be tough to step through the inlined functions.
Well yeah, that's it. I removed the operator and everything went a mighty bit faster.

I then switched it to Release and the speed went from 3.2 to 0.8 seconds. Good Jesus with a baseball bat.
Topic archived. No new replies allowed.