Return speed

M

Michael

If I am going to be using the same class over and over again passing between
functions, would the following be quicker (as the constructor wouldn't have
to be called):

//First Example:

class MVector{
float x,y,z;
MVector() {x=y=z=0);
};

class MBoundingBox
{
MVector GetCentreVector() {/* Gets Vector */}
};

MVector vec;
MBoundingBox BB[1000];


for(int i=0;i<10000;i++)
{
vec = BB.GetCentreVector();
/* do something */
}






//Second Example:

class MVector{
float x,y,z;
MVector() {x=y=z=0);
};

class MBoundingBox
{
void GetCentreVector(MVector& vec) { /* put vec into reference */}
};

MVector vec;
MBoundingBox BB[1000];


for(int i=0;i<10000;i++)
{
BB.GetCentreVector(vec);
/* do something */
}



ie is it quicker to pass by reference than to return by value??
Regards

Michael
 
V

Victor Bazarov

Michael said:
If I am going to be using the same class over and over again passing between
functions, would the following be quicker (as the constructor wouldn't have
to be called):

//First Example:

class MVector{
float x,y,z;

public:

MVector() {x=y=z=0);

MVector() : x(0), y(0), z(0) {}
};

class MBoundingBox
{
public:

MVector GetCentreVector() {/* Gets Vector */}
};

MVector vec;
MBoundingBox BB[1000];


for(int i=0;i<10000;i++)
{
vec = BB.GetCentreVector();


vec = BB.GetCentreVector();
/* do something */
}






//Second Example:

class MVector{
float x,y,z;

public:

MVector() {x=y=z=0);

MVector() : x(0), y(0), z(0) {}
};

class MBoundingBox
{
public:

void GetCentreVector(MVector& vec) { /* put vec into reference */}
};

MVector vec;
MBoundingBox BB[1000];


for(int i=0;i<10000;i++)
{
BB.GetCentreVector(vec);
BB.GetCentreVector(vec);

/* do something */
}



ie is it quicker to pass by reference than to return by value??


Usually, yes. In your particular case probably the same, the compiler
is allowed to do what's known as "return value optimization". Look it
up.

Victor
 
I

Iulian Dragos

class MVector{
float x,y,z;
MVector() {x=y=z=0);
};

class MBoundingBox
{
MVector GetCentreVector() {/* Gets Vector */}
};

MVector vec;
MBoundingBox BB[1000];


for(int i=0;i<10000;i++)
{
vec = BB.GetCentreVector();
/* do something */
}


Maybe "vec = BB.GetCentreVector()" ?

[....]
ie is it quicker to pass by reference than to return by value??
Regards

I guess it depends on your compiler.. but you have both programs! Why don't
you time them and see for yourself? It would be interesting to post the
results afterwards.

I would say (based on my limited compiler construction knowledge) that
passing the reference is quicker: less memory operations and might even be
passed in a register. I'm not sure whether you can use the reference as an
"out" parameter, but you can certainly return a reference (like in the first
example).

As a side note, in your first example the (default) copy constructor IS
called.

A second side note: as you mentioned, you need to pass these structures a
lot between various functions. Why not make them member functions in your
structures?

HTH,
iuli
 
J

John Harrison

Michael said:
If I am going to be using the same class over and over again passing between
functions, would the following be quicker (as the constructor wouldn't have
to be called):

[snip]


ie is it quicker to pass by reference than to return by value??
Regards

The C++ language does not specify the speed of any operations. The answer to
your question could easily depend on which compiler and which machine. The
ONLY way to answer questions like this is to try both methods and time the
results.

john
 
E

E. Robert Tisdale

Michael said:
If I am going to be using the same class over and over again passing between
functions, would the following be quicker
(as the constructor wouldn't have to be called):

//First Example:

class MVector{
private:
// representation
float x, y, z;
public:
// constructors
MVector(void): x(), y(), z() { }
};

class MBoundingBox {
MVector GetCentreVector(void) {/* Gets Vector */}
};

MVector vec;
MBoundingBox BB[1000];

for(int i = 0; i < 10000; ++i) {
vec = BB.GetCentreVector();
/* do something */
}

//Second Example:

class MVector {

private:
// representation
float x,y,z;
public:
// constructors
MVector(void): x(), y(), z() { }
};

class MBoundingBox { public:
void GetCentreVector(MVector& vec) { /* put vec into reference */}
};

MVector vec;
MBoundingBox BB[1000];


for(int i = 0; i < 10000; ++i) {
BB.GetCentreVector(vec);
/* do something */
}

i.e. Is it quicker to pass by reference than to return by value?


No.
 

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

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top