Reference vs. pointers

Oct 11, 2010 at 9:14pm
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.
Oct 11, 2010 at 9:37pm
Is your operator[] inlined?

I'm betting you're spending all your time calling that function and returning.
Oct 11, 2010 at 9:39pm
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.
Oct 11, 2010 at 9:39pm
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 Oct 11, 2010 at 9:40pm
Oct 11, 2010 at 9:42pm
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 Oct 11, 2010 at 9:43pm
Oct 11, 2010 at 9:43pm
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.
Oct 11, 2010 at 9:43pm
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).
Oct 11, 2010 at 9:46pm
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?
Oct 11, 2010 at 9:50pm
Probably not. Otherwise it would be tough to step through the inlined functions.
Oct 11, 2010 at 10:08pm
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.