473,890 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Operator overloading, C++ performance crappiness

Is there any way to get to the left-hand side of an operator? Consider
the following (this is not meant to be perfect code, just an example of
the problem):

class Matrix
{
public:
int data[1024];

Matrix() {}

Matrix(int value)
{
for (unsigned i = 0; i < sizeof(data)/sizeof(int); i++)
data[i] = value;
}

void add(const Matrix& obj, Matrix* output)
{
for (unsigned i = 0; i < sizeof(data)/sizeof(int); i++)
output->data[i] = data[i] + obj.data[i];
}

Matrix operator +(const Matrix& obj)
{
Matrix temp; // "unnecessar y" creation of temp variable

for (unsigned i = 0; i < sizeof(data)/sizeof(int); i++)
temp.data[i] = data[i] + obj.data[i];

return temp; // "unnecessar y" extra copy of output
}
};

For nice looking syntax you _really_ want to use the operator+ like:
matrix3 = matrix1 + matrix2;

However, that is some 50% slower than the _much_ uglier:
matrix1.add(mat rix2, &matrix3);

If only there were a way to get to the left-hand argument of the
operator+ then it could be fast and easy to use. Consider the following
code which is not valid C++ and will not compile for this example:

Matrix as M
operator+(const Matrix& obj)
{
for (unsigned i = 0; i < sizeof(data)/sizeof(int); i++)
M.data[i] = data[i] + obj.data[i];
}

That would be fast and clean to use. Is there any way to accomplish
this? Otherwise the situation is just ugly and there is no point in
using operator overloading for these types of situations (which really
defeats the purpose of operator overloading in the first place).

Thanks! Jo
Aug 17 '05 #1
51 3619

"Jojo" <jo**@pleasenom orespamicanttak eitanymore.com> schrieb im Newsbeitrag
news:43******** *************** @authen.white.r eadfreenews.net ...
Is there any way to get to the left-hand side of an operator? Consider
the following (this is not meant to be perfect code, just an example of
the problem):

class Matrix
{
public:
int data[1024];

Matrix() {}

Matrix(int value)
{
for (unsigned i = 0; i < sizeof(data)/sizeof(int); i++)
data[i] = value;
}

void add(const Matrix& obj, Matrix* output)
{
for (unsigned i = 0; i < sizeof(data)/sizeof(int); i++)
output->data[i] = data[i] + obj.data[i];
}

Matrix operator +(const Matrix& obj)
{
Matrix temp; // "unnecessar y" creation of temp variable

for (unsigned i = 0; i < sizeof(data)/sizeof(int); i++)
temp.data[i] = data[i] + obj.data[i];

return temp; // "unnecessar y" extra copy of output
}
};

For nice looking syntax you _really_ want to use the operator+ like:
matrix3 = matrix1 + matrix2;

However, that is some 50% slower than the _much_ uglier:
matrix1.add(mat rix2, &matrix3);

If only there were a way to get to the left-hand argument of the
operator+ then it could be fast and easy to use. Consider the following
code which is not valid C++ and will not compile for this example:

Matrix as M
operator+(const Matrix& obj)
{
for (unsigned i = 0; i < sizeof(data)/sizeof(int); i++)
M.data[i] = data[i] + obj.data[i];
}

That would be fast and clean to use. Is there any way to accomplish
this? Otherwise the situation is just ugly and there is no point in
using operator overloading for these types of situations (which really
defeats the purpose of operator overloading in the first place).

Thanks! Jo


You could just use operator+=
matrix1 += matrix2;
matrix1 += matrix3;

or rewrite add()
matrix1.add(mat rix2);
matrix1.add(mat rix3);

or you could write the function add using ellipses:
add(...);

matrix1.add(&ma trix2, &matrix3, &matrix4, &matrix5);

Greets Chris
Aug 17 '05 #2
Christian Meier wrote:
You could just use operator+=
matrix1 += matrix2;
matrix1 += matrix3;

or rewrite add()
matrix1.add(mat rix2);
matrix1.add(mat rix3);

or you could write the function add using ellipses:
add(...);

matrix1.add(&ma trix2, &matrix3, &matrix4, &matrix5);

Greets Chris


"+=" does not accomplish the same thing, and neither does add(Matrix).
There is a third variable involved which is the result of adding two
other variables that you don't want to modify.

As I mentioned the "matrix.add ()" syntax certainly works and it is fast
but it is extremely awkward to use and makes for some nasty looking code.

Jo
Aug 17 '05 #3
> Is there any way to get to the left-hand side of an operator? Consider
the following (this is not meant to be perfect code, just an example of
the problem):

class Matrix
{
public:
int data[1024];

Matrix() {}

Matrix(int value)
{
for (unsigned i = 0; i < sizeof(data)/sizeof(int); i++)
data[i] = value;
}

void add(const Matrix& obj, Matrix* output)
{
for (unsigned i = 0; i < sizeof(data)/sizeof(int); i++)
output->data[i] = data[i] + obj.data[i];
}

Matrix operator +(const Matrix& obj)
{
Matrix temp; // "unnecessar y" creation of temp variable

for (unsigned i = 0; i < sizeof(data)/sizeof(int); i++)
temp.data[i] = data[i] + obj.data[i];

return temp; // "unnecessar y" extra copy of output
}
};

For nice looking syntax you _really_ want to use the operator+ like:
matrix3 = matrix1 + matrix2;

However, that is some 50% slower than the _much_ uglier:
matrix1.add(mat rix2, &matrix3);

If only there were a way to get to the left-hand argument of the operator+
then it could be fast and easy to use. Consider the following code which
is not valid C++ and will not compile for this example:

Matrix as M
operator+(const Matrix& obj)
{
for (unsigned i = 0; i < sizeof(data)/sizeof(int); i++)
M.data[i] = data[i] + obj.data[i];
}

That would be fast and clean to use. Is there any way to accomplish this?
Otherwise the situation is just ugly and there is no point in using
operator overloading for these types of situations (which really defeats
the purpose of operator overloading in the first place).

Thanks! Jo


If that code is slow, that's because it is not well written. In production
code you probably don't want to return a full-blown matrix object from
operator + because that'd make a temporary. Instead, a small object of some
other type is returned that records the operation and operands. The
evaluation happens eventually in the assignment operation.

class MatrixOp;

MatrixOp operator + (const Matrix&, const Matrix);
Matrix operator = (Matrix&, const MatrixOp);

Ben
Aug 17 '05 #4
"Jojo" <jo**@pleasenom orespamicanttak eitanymore.com> schrieb im Newsbeitrag
news:43******** *************** @authen.white.r eadfreenews.net ...
Christian Meier wrote:
You could just use operator+=
matrix1 += matrix2;
matrix1 += matrix3;

or rewrite add()
matrix1.add(mat rix2);
matrix1.add(mat rix3);

or you could write the function add using ellipses:
add(...);

matrix1.add(&ma trix2, &matrix3, &matrix4, &matrix5);

Greets Chris
"+=" does not accomplish the same thing, and neither does add(Matrix).
There is a third variable involved which is the result of adding two
other variables that you don't want to modify.


Why not? After your statements an object has the value of the sum of two
others.
obj1 = obj2 + obj3;
obj1 += obj2;
obj1 += obj3;

If you need to reset obj1 first, you can write a reset() function. Or assign
0 before the +=operator calls.

As I mentioned the "matrix.add ()" syntax certainly works and it is fast
but it is extremely awkward to use and makes for some nasty looking code.

Jo
Is sizeof(data)/sizeof(int) not the same for these three objects?
If not, then you have problems with your function add():
void add(const Matrix& obj, Matrix* output)
{
for (unsigned i = 0; i < sizeof(data)/sizeof(int); i++)
output->data[i] = data[i] + obj.data[i];
}


You do no range checking here for output->data and obj.data.

Greets Chris
Aug 17 '05 #5
benben wrote:
If that code is slow, that's because it is not well written. In production
code you probably don't want to return a full-blown matrix object from
operator + because that'd make a temporary. Instead, a small object of some
other type is returned that records the operation and operands. The
evaluation happens eventually in the assignment operation.

class MatrixOp;

MatrixOp operator + (const Matrix&, const Matrix);
Matrix operator = (Matrix&, const MatrixOp);

Ben


You are still going to incur a performance penalty from copying data
into the MatrixOp variable. This penalty is certainly much smaller than
that of copying the objects in my example but as I said in the beginning
my example is to illustrate the problem and not be an example of perfect
code used in production.

My very last pseudo code for operator+ would still be faster than using
a MatrixOp temp variable and the "Matrix.add(con st Matrix&, Matrix*
output)" is faster as well (although extemely ugly as I already mentioned).

Jo
Aug 17 '05 #6
Jojo wrote:
Is there any way to get to the left-hand side of an operator? Consider
the following (this is not meant to be perfect code, just an example of
the problem):

class Matrix
{
public:
int data[1024];

Matrix() {}

Matrix(int value)
{
for (unsigned i = 0; i < sizeof(data)/sizeof(int); i++)
data[i] = value;
}

void add(const Matrix& obj, Matrix* output)
{
for (unsigned i = 0; i < sizeof(data)/sizeof(int); i++)
output->data[i] = data[i] + obj.data[i];
}

Matrix operator +(const Matrix& obj)
{
Matrix temp; // "unnecessar y" creation of temp variable

for (unsigned i = 0; i < sizeof(data)/sizeof(int); i++)
temp.data[i] = data[i] + obj.data[i];

return temp; // "unnecessar y" extra copy of output
}
};

For nice looking syntax you _really_ want to use the operator+ like:
matrix3 = matrix1 + matrix2;

However, that is some 50% slower than the _much_ uglier:
matrix1.add(mat rix2, &matrix3);

If only there were a way to get to the left-hand argument of the
operator+ then it could be fast and easy to use.
you would want to get to left hand argument of operator=, but then how
do you know its there ?

what about:

foo( a + b );

operator+ would need to know about the context its used in than ...
Consider the following
code which is not valid C++ and will not compile for this example:

Matrix as M
operator+(const Matrix& obj)
{
for (unsigned i = 0; i < sizeof(data)/sizeof(int); i++)
M.data[i] = data[i] + obj.data[i];
}

That would be fast and clean to use. Is there any way to accomplish
this? Otherwise the situation is just ugly and there is no point in
using operator overloading for these types of situations (which really
defeats the purpose of operator overloading in the first place).

Thanks! Jo

Aug 17 '05 #7
> You are still going to incur a performance penalty from copying data into
the MatrixOp variable. This penalty is certainly much smaller than that
of copying the objects in my example but as I said in the beginning my
example is to illustrate the problem and not be an example of perfect code
used in production.


MatrixOp can be constructed without copying any matrix at all. A reference
or pointer will do.
Ben
Aug 17 '05 #8
benben wrote:
You are still going to incur a performance penalty from copying data into
the MatrixOp variable. This penalty is certainly much smaller than that
of copying the objects in my example but as I said in the beginning my
example is to illustrate the problem and not be an example of perfect code
used in production.

MatrixOp can be constructed without copying any matrix at all. A reference
or pointer will do.
Ben


I didn't say anything to the contrary. The "add(const Object& obj,
Object* output)" method in my example is still faster. MatrixOp adds
overhead no matter how you look at it. Like I said before, it's a
smaller overhead than copying a large object but it _does_ add overhead.

Consider if we're working with millions of "Vector" objects that only
have two or three float members. MatrixOp would have about the same
complexity as the Vector object itself. There would be no benefit to
using it and it would be slower than the ever so ugly:

void add(const Vector& obj, Vector* output)
{
*output = obj;
}

Jo
Aug 17 '05 #9
Christian Meier wrote:
Why not? After your statements an object has the value of the sum of two
others.
obj1 = obj2 + obj3;
obj1 += obj2;
obj1 += obj3;
That is not going to be any faster than the plain slow operator+ in my
example. First you "add" (copy) obj2 into obj1, then you add obj3.
This is slower than just adding two variables straight out into a third.
If you need to reset obj1 first, you can write a reset() function. Or assign
0 before the +=operator calls.


Again, another performance hit that is going to be slower than just
adding two objects straight out into a (uninitialized) third.

Jo
Aug 17 '05 #10

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

Similar topics

9
2865
by: Steve Sargent | last post by:
Hi: I'm trying to debug the following code, and it keeps looping on the if statement: public static bool operator == (OnlineMemberNode first, OnlineMemberNode second) { if(first == null) {
17
2523
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: public static bool operator !=(MyType x, MyType y) { return !(x == y); } That way the == operator handles everything, and extra comparing logic isn't
0
11214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10801
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10903
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9616
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8006
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7158
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6034
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4659
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.