效益分析 [英] Effeciency

查看:40
本文介绍了效益分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个无符号整数的东西,现在只允许按位运算符

。我已经定义了这些操作符和他们的

自我分配的表兄弟。


这有效,但我想知道什么是最有效的方式

这样做是。优选地,与

读取unsigned long int相比,应该没有开销。没有额外的临时工具,完整的内联等。

应该生成相同的代码。


unsigned long int和我的类之间的唯一区别应该是

它是它自己独立的类型,我控制什么操作和

转换是可能的/自动的。


class MyClass

{

public:

MyClass(){}

显式MyClass(unsigned long int uli)< br $>
{

bb = uli;

}


MyClass& operator& =(const MyClass& rhs){bb& = rhs.bb;返回

* this; }

MyClass& operator | =(const MyClass& rhs){bb | = rhs.bb;返回

* this; }

MyClass& operator ^ =(const MyClass& rhs){bb ^ = rhs.bb;返回

* this; } $ / $

MyClass运算符&(const MyClass& rhs){return MyClass(bb& rhs.bb); } $ / $
MyClass运算符|(const MyClass& rhs){return MyClass(bb | rhs.bb); } $ / $
MyClass运算符^(const MyClass& rhs){return MyClass(bb ^ rhs.bb); }


私人:

unsigned long int bb;

};


/ David

I want to have an unsigned integer-like thing, that for now only allows
the bitwise operators. I have defined these operators and their
self-assigning cousins.

This works, but I would like to know what the most effecient way of
doing this is. Preferably, there should be no overhead compared to a
read unsigned long int. No extra temporaries, full inlining etc. The
same code should be generated.

The only difference between an unsigned long int and my class should be
that it is it''s own separate type and that I control what operations and
conversions are possible/automatic.

class MyClass
{
public:
MyClass() {}
explicit MyClass(unsigned long int uli)
{
bb = uli;
}

MyClass& operator &=(const MyClass& rhs) { bb &= rhs.bb; return
*this; }
MyClass& operator |=(const MyClass& rhs) { bb |= rhs.bb; return
*this; }
MyClass& operator ^=(const MyClass& rhs) { bb ^= rhs.bb; return
*this; }

MyClass operator &(const MyClass& rhs) { return MyClass(bb & rhs.bb); }
MyClass operator |(const MyClass& rhs) { return MyClass(bb | rhs.bb); }
MyClass operator ^(const MyClass& rhs) { return MyClass(bb ^ rhs.bb); }

private:
unsigned long int bb;
};

/David

推荐答案

2004年1月26日星期一02:33:22 +0100,David Rasmussen< da ******** *****@gmx.net>写道:
On Mon, 26 Jan 2004 02:33:22 +0100, David Rasmussen <da*************@gmx.net> wrote:
我想要一个无符号整数类的东西,现在只允许按位运算符。我已经定义了这些操作符及其自我分配的表兄弟。

这有效,但我想知道这样做最有效的方法是什么。优选地,与读取unsigned long int相比,应该没有开销。没有额外的临时工具,完整的内联等。应该生成相同的代码。

unsigned long int和我的班级之间的唯一区别应该是
它是''我自己的单独类型,我控制哪些操作和/或转换是可能的/自动的。
I want to have an unsigned integer-like thing, that for now only allows
the bitwise operators. I have defined these operators and their
self-assigning cousins.

This works, but I would like to know what the most effecient way of
doing this is. Preferably, there should be no overhead compared to a
read unsigned long int. No extra temporaries, full inlining etc. The
same code should be generated.

The only difference between an unsigned long int and my class should be
that it is it''s own separate type and that I control what operations and
conversions are possible/automatic.




这是一个实施质量问题。


C ++正式允许编译器产生任何数量的开销。


如果你对操作的控制能力稍差,那么它可能是

想要将枚举类型视为您的类型(只是一个想法)。



That is a quality-of-implementation issue.

C++ formally allows the compiler to generate any amount of overhead.

If you can live with somewhat less control over operations it might be
an idea to consider an enum-type as your type (just a thought).


David Rasmussen写道:
David Rasmussen wrote:
我想要一个无符号整数的东西,现在只允许按位运算符。我已经定义了这些操作符及其自我分配的表兄弟。

这有效,但我想知道这样做最有效的方法是什么。优选地,与读取unsigned long int相比,应该没有开销。没有额外的临时工具,完整的内联等。应该生成相同的代码。

unsigned long int和我的班级之间的唯一区别应该是
它是''我自己独立的类型,我控制什么操作和/或转换是可能的/自动的。

类MyClass
{
公共:
MyClass(){ }
显式MyClass(unsigned long int uli)
{
bb = uli;
}


.... knit - best进入使用初始化程序列表的习惯

ie:

显式MyClass(unsigned long int uli)

:bb(uli)

{

}


你缺少一个复制构造函数:

MyClass(const MyClass) &);


....和默认构造函数


MyClass();


....一个任务操作员。


MyClass& operator =(const MyClass& rhs);


如果没有上述功能,很难做很多事情。


就性能而言去 - 下面的操作员似乎很好。

MyClass& operator& =(const MyClass& rhs){bb& = rhs.bb;返回
*这个; }
MyClass& operator | =(const MyClass& rhs){bb | = rhs.bb;返回
*这个; }
MyClass& operator ^ =(const MyClass& rhs){bb ^ = rhs.bb;返回
*这个; }


这看起来和你一样好。唯一挑剔的问题是

,你复制了上面的功能,但你可能不会因此而在这里得到任何问题。

MyClass运算符&(const MyClass& rhs){return MyClass(bb& rhs.bb); }

MyClass运算符|(const MyClass& rhs){return MyClass(bb | rhs.bb); }

MyClass运算符^(const MyClass& rhs){return MyClass(bb ^ rhs.bb); }

私人:
unsigned long int bb;
};
I want to have an unsigned integer-like thing, that for now only allows
the bitwise operators. I have defined these operators and their
self-assigning cousins.

This works, but I would like to know what the most effecient way of
doing this is. Preferably, there should be no overhead compared to a
read unsigned long int. No extra temporaries, full inlining etc. The
same code should be generated.

The only difference between an unsigned long int and my class should be
that it is it''s own separate type and that I control what operations and
conversions are possible/automatic.

class MyClass
{
public:
MyClass() {}
explicit MyClass(unsigned long int uli)
{
bb = uli;
}
.... knit - best to get into the habbit of using initializer lists
i.e.:
explicit MyClass(unsigned long int uli)
: bb(uli)
{
}

You lack a copy constructor:

MyClass( const MyClass & );

.... and a default constructor

MyClass();

.... an an assignment operator.

MyClass& operator= (const MyClass& rhs);

It''s hard to do much without the above functions.

As far as performance goes - the operators below seem fine.

MyClass& operator &=(const MyClass& rhs) { bb &= rhs.bb; return
*this; }
MyClass& operator |=(const MyClass& rhs) { bb |= rhs.bb; return
*this; }
MyClass& operator ^=(const MyClass& rhs) { bb ^= rhs.bb; return
*this; }

This looks about as good as you could as well. The only picky issue is
that you''re duplicating the functionality above but you''re likely not
going to get any issues here because of that.
MyClass operator &(const MyClass& rhs) { return MyClass(bb & rhs.bb); }
MyClass operator |(const MyClass& rhs) { return MyClass(bb | rhs.bb); }
MyClass operator ^(const MyClass& rhs) { return MyClass(bb ^ rhs.bb); }

private:
unsigned long int bb;
};








Alf P. Steinbach写道:
Alf P. Steinbach wrote:

这是一个实施质量问题。


我知道,但......

C ++正式允许编译器生成任何数量的开销。


我知道,但是......


也许我应该改写一下:我想确保我没有做到这一点

不必要的低效率。我想给编译器最好的工作状态

条件。

如果你可以对操作稍微控制一点,那么可能会考虑枚举-type作为你的类型(只是一个想法)。

That is a quality-of-implementation issue.

I know, but...
C++ formally allows the compiler to generate any amount of overhead.

I know, but...

Maybe I should rephrase it: I want to make sure that I don''t make it
unnecesarily inefficient. I want to give the compiler the best working
conditions.
If you can live with somewhat less control over operations it might be
an idea to consider an enum-type as your type (just a thought).




我不能,但感谢你的建议:)


/ David



I can''t, but thanks for the suggestion :)

/David


这篇关于效益分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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