运算符重载,C ++性能不稳定 [英] Operator overloading, C++ performance crappiness

查看:70
本文介绍了运算符重载,C ++性能不稳定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法到达运营商的左侧?考虑下面的

(这不是完美的代码,只是一个例子

问题):


class矩阵

{

public:

int data [1024];


Matrix(){ }


矩阵(int值)

{

for(无符号i = 0; i< sizeof(data)/ sizeof(int); i ++)

data [i] = value;

}


void add(const Matrix& obj ,Matrix *输出)

{

for(unsigned i = 0; i< sizeof(data)/ sizeof(int); i ++)

output-> data [i] = data [i] + obj.data [i];

}


矩阵运算符+(const Matrix& ; obj)

{

Matrix temp; //不必要的创建临时变量


for(unsigned i = 0; i< sizeof(data)/ sizeof(int); i ++)

temp.data [ i] = data [i] + obj.data [i];


return temp; //不必要的额外的输出副本

}

};


对于漂亮的语法,你_really_想要使用运算符+如:

matrix3 = matrix1 + matrix2;


然而,这比_much_ uglier慢了约50%:

matrix1.add(matrix2 ,& matrix3);


如果只有一种方法可以获得

运算符的左手参数+那么它可以快速而简单使用。请考虑以下

代码,它不是有效的C ++,不会为此示例编译:


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];

}


那将是快速和干净的使用。有没有办法完成

这个?否则情况只是丑陋而且在这些类型的情况下使用运算符重载没有任何意义(这实际上是因为
首先违背了运算符重载的目的) 。


谢谢! Jo

解决方案



" Jojo" <乔** @ pleasenomorespamicanttakeitanymore.com> schrieb im Newsbeitrag

news:43 *********************** @ authen.white.readfr eenews.net ... < blockquote class =post_quotes>有没有办法到达运营商的左侧?考虑以下(这不是完美的代码,只是问题的一个例子):

类矩阵
{
公开:
int数据[1024];

矩阵(){}

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

矩阵运算符+(const Matrix& obj)
{
Matrix temp; //不必要的创建临时变量

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

返回临时; //不必要的额外的输出副本
}
};

为了看起来很漂亮的语法你_really_想要使用运算符+如:
matrix3 = matrix1 + matrix2;

然而,这比_much_ uglier慢了约50%:
matrix1.add(matrix2,& matrix3);

如果只有一种方法可以到达
运算符的左手参数+然后它可以快速且易于使用。请考虑以下代码,这些代码不是有效的C ++,并且不会为此示例编译:

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];
}

这将是快速和干净使用。有没有办法完成这个?否则情况只是丑陋而且没有必要在这些类型的情况下使用运算符重载(这实际上首先会破坏运算符重载的目的)。
谢谢! Jo




您可以使用operator + =

matrix1 + = matrix2;

matrix1 + = matrix3;


或重写add()

matrix1.add(matrix2);

matrix1.add(matrix3);


或者您可以使用省略号编写函数add:

add(...);


matrix1.add(& ; matrix2,& matrix3,& matrix4,& matrix5);


Greets Chris


Christian Meier写道:< blockquote class =post_quotes>你可以使用operator + =
matrix1 + = matrix2;
matrix1 + = matrix3;

或重写add()
matrix1。添加(matrix2);
matrix1.add(matrix3);

或者您可以使用省略号编写函数add:
add(...);

matrix1.add(& matrix2,& matrix3,& matrix4,& matrix5);

Greets Chris




" + ="没有完成相同的事情,也没有添加(矩阵)。

涉及第三个变量,这是因为你要添加两个

其他变量我想修改。


正如我提到的那样,matrix.add()语法肯定有效,而且速度很快

但是它使用起来非常笨拙并且会产生一些讨厌的代码。


Jo

>有没有办法到达运营商的左侧?考虑

以下(这不是完美的代码,只是问题的一个例子):

类Matrix
{
public:
int data [1024];

Matrix(){}

Matrix(int value)

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

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

矩阵运算符+(const Matrix& obj)
{
Matrix temp; //不必要的创建临时变量

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

返回临时; //不必要的额外的输出副本
}
};

为了看起来很漂亮的语法你_really_想要使用运算符+如:
matrix3 = matrix1 + matrix2;

然而,这比_much_ uglier慢了约50%:
matrix1.add(matrix2,& matrix3);

如果只有一种方法可以到达操作员的左手参数+
然后它可以快速且易于使用。请考虑以下代码,它们不是有效的C ++,并且不会为此示例编译:

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];
}

这将是快速和干净使用。有没有办法实现这个目标?
否则情况就是丑陋而且对于这些类型的情况使用
操作符重载是没有意义的(这实际上会破坏运算符重载的目的)第一名)。

谢谢! Jo




如果代码很慢,那是因为它编写得不好。在生产

代码中,你可能不想从

运算符+返回一个完整的矩阵对象,因为那会是暂时的。相反,返回一些其他类型的小对象,记录操作和操作数。

评估最终在分配操作中发生。


class MatrixOp;


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

Matrix operator =(Matrix&,const MatrixOp);


Ben


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; // "unnecessary" creation of temp variable

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

return temp; // "unnecessary" 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(matrix2, &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

解决方案


"Jojo" <jo**@pleasenomorespamicanttakeitanymore.com> schrieb im Newsbeitrag
news:43***********************@authen.white.readfr eenews.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; // "unnecessary" creation of temp variable

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

return temp; // "unnecessary" 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(matrix2, &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(matrix2);
matrix1.add(matrix3);

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

matrix1.add(&matrix2, &matrix3, &matrix4, &matrix5);

Greets Chris


Christian Meier wrote:

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

or rewrite add()
matrix1.add(matrix2);
matrix1.add(matrix3);

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

matrix1.add(&matrix2, &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


> 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; // "unnecessary" creation of temp variable

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

return temp; // "unnecessary" 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(matrix2, &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


这篇关于运算符重载,C ++性能不稳定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆