473,386 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Return speed

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[i]();
/* 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[i](vec);
/* do something */
}

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

Michael
Jul 22 '05 #1
4 1479
"Michael" <sl***********@hotmail.com> wrote...
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[i]();
vec = BB[i].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[i](vec);
BB[i].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
Jul 22 '05 #2
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[i]();
/* do something */
}
Maybe "vec = BB[i].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
Jul 22 '05 #3

"Michael" <sl***********@hotmail.com> wrote in message
news:c8**********@titan.btinternet.com...
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
Jul 22 '05 #4
Michael wrote:
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[i].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[i](vec);
/* do something */
}

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


No.

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: nakhi | last post by:
Hi, I updated to aspx last three month,after developing serveral applications,Idecided to downgraded to asp, following is my reasons: 1.in ASPX,the page format is hard to control,in ASP u can...
2
by: Chris | last post by:
I think I already know that the answer is that this can't be done, but I'll ask anyways. Suppose you want to use an RDBMS to store messages for a threaded message forum like usenet and then...
7
by: YAZ | last post by:
Hello, I have a dll which do some number crunching. Performances (execution speed) are very important in my application. I use VC6 to compile the DLL. A friend of mine told me that in Visual...
25
by: frizzle | last post by:
Hi there, I have a mySQL system with a news publishing part in it: Admins can create new items with text in it, and they have an option to create 'fulltexts', so you'd get "read more ..." on the...
40
by: Mark P | last post by:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost...
18
by: SpiralCorp | last post by:
int divide (int a, int b) { int r; r=a/b; return (r); } int main () { int result = divide (20,4); cout << result
3
by: Eric Layman | last post by:
Hi, Are there ways for customvalidator to have 2 different error messages? eg: i have 2 conditions to check using customvalidator depends on condition, i would need to return 2 different...
11
by: kyosohma | last post by:
Hi, We use a script here at work that runs whenever someone logs into their machine that logs various bits of information to a database. One of those bits is the CPU's model and speed. While...
4
by: nestle | last post by:
I have DSL with a download speed of 32MB/s and an upload speed of 8MB/s(according to my ISP), and I am using a router. My upload speed is always between 8MB/s and 9MB/s(which is above the max upload...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.