Sign in to follow this  
thedodgeruk

is there a speed differnce between returning a pointer and rreference?

Recommended Posts

i have classes which include classes
the main class is done with pointers and use "new" to create , but the classes inside are done as as concreate

class CPosition
{
D3DXVECTOR 3 pos;
D3DXVECTOR dir;
};


ie
class CModel
{
CPosition m_position;
CPosition& GetPosition() { return m_position}

}

Cmodel * model = new CModel;

D3DXVECTO3 model->GetPosition.pos;

how much slower is this way , compaired to if i made CPosition m_position; into CPosition* m_position;


trying to get the max speed , but using new is slow as well ?

Share this post


Link to post
Share on other sites
[quote name='thedodgeruk' timestamp='1318802649' post='4873227']
how much slower is this way , compaired to if i made CPosition m_position; into CPosition* m_position;
[/quote]
It's probably faster (using CPosition m_position is probably faster, I mean), actually. Allocating and deallocating memory (via new and delete, or malloc and free) are very expensive operations, which you would have to do for CPosition* m_position. Plus you would have to dereference the pointer every time in order to access it, again costing you a cycle or two (or however many, but not too much) in the CPU.

In terms of processing in the CPU, accessing memory is one of the most expensive things you can do. Once the memory is loaded into the registers of the CPU, it's easy and quick to process it and do whatever mathematical manipulations you need to. With either option, you'll have to access memory, but you have to access and manipulate more memory with pointers.

Share this post


Link to post
Share on other sites
Returning a pointer or returning a reference shouldn't have any impact on performance. GetPosition() can return a pointer or a reference independent on how you store m_position inside the object.

Share this post


Link to post
Share on other sites
Keep it as a member, there's no point in allocating it separately.
You can return a pointer to your m_position even though it's not stored as a pointer, simply take its address (&m_position).
However, I think most people would prefer that it returned a reference, since a a reference cannot be null like a pointer can be (usually). You may want to return a const reference though.

You should probably stop worrying about minor speed differences things like these make if you want to make your program fast.
Make your program, measure its speed, and only if it is not fast enough you should try optimizing it.
You should also focus on optimizing your algorithms, an extra float operation or an unneeded copy of a position will most likely not even show up when profiling.

Share this post


Link to post
Share on other sites
...and don't forget to actually measure your performance and ensure that you're certain that this stuff is actually a bottleneck for your program before even worrying about it. I'm willing to bet that 99 times out of 100 it isn't (the devil is in that other 1 time).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this