4
A
回答
7
理想情况下,您可以剖析代码然后决定内联。当你决定内联常规运营商时,实际上没有太大区别。
6
如果您正在添加大向量,则相对于实际添加两个向量的时间而言,函数调用加号的开销将较小。因此,标记运营商+内联不可能改善您的整体运行时间。
+0
......或者至少不够关心。 –
+1
如果经常使用操作员,内嵌它会增加代码大小,并可能由于缓存缺失而降低效率。 – Kleist
4
让编译器决定优化。
关键字inline
是一种误导:编译器可以-in实况永远做它需要的,就像与老auto
(你remenber那些日子?)和register
。
它的现代意义是“定义在标题:丢弃,如果不使用,合并,如果看到更多次”。
1
编译器应该在发布版本中自动为您内联小函数。 更重要的是定义一个移动构造函数并移动赋值。如果你的数组非常大,并且你同时执行多个操作,你还可以使用表达式类来提高执行速度。
template <class left, class right>
struct AddExpr {
const left& _left;
const right& _right;
AddExpr(const left& Left, const right& Right)
:_left(Left), _right(Right)
{assert(left.count() == right.count());}
int count() const {return _left.count();}
int operator[](int index) const {return _left[i]+_right[i];}
};
class Array {
int* data;
int size;
int count() const {return size;}
Array& operator=(AddExpr expr) {
for(int i=0; i<expr.count(); ++i)
data[i] = expr[i];
};
AddExpr operator+(const Array& lhs, const Array& rhs)
{return AddExpr<Array, Array>(lhs, rhs);}
AddExpr operator+(const Array& lhs, const Expr& rhs)
{return AddExpr<Array, Expr>(lhs, rhs);}
AddExpr operator+(const Expr& lhs, const Array& rhs)
{return AddExpr<Expr, Array>(lhs, rhs);}
AddExpr operator+(const Expr& lhs, const Expr& rhs)
{return AddExpr<Expr, Expr>(lhs, rhs);}
int main() {
Array a, b, c, d;
Array c = (a+b) + (c+d); //awesome on lines like this
}
这将删除所有临时对象,并大大提高缓存效率。但是我完全忘记了这种技术被称为什么。
相关问题
- 1. C++运算符重载利用关联
- 2. C++运算符在已重载的运算符中重载
- 3. C++运算符重载==
- 4. 运算符重载C++
- 5. 运算符重载C++
- 6. 运算符重载C++
- 7. 运算符重载C++
- 8. C++运算符重载
- 9. +运算符重载在C++
- 10. C++约运算符重载
- 11. 运算符重载 - 重载*
- 12. C#字符串运算符重载
- 13. 级联重载+运算
- 14. 重载运算符*
- 15. 重载运算符%
- 16. 重载++运算符
- 17. 重载+ =运算符
- 18. 运算符重载?
- 19. 重载运算符[]
- 20. 重载运算符=
- 21. 重载=运算符
- 22. 运算符重载(=)
- 23. 运算符重载
- 24. 重载运算符+
- 25. 运算符重载+ =
- 26. 内置类型的运算符重载
- 27. 重载<运算符类内部
- 28. 重载内置类型的运算符
- 29. 运算符在OpenCL内核中重载
- 30. 运算符在C++帮助中重载流提取运算符
+1。和@Gaurish Telang:内联适配器方法(为兼容性提供的方法,例如使用相同参数调用其他方法)是合乎逻辑的。 –
+1。在我们的团队工作中,我们的编码标准不鼓励在头文件中内联方法,除非有可证明的性能增益。我们也劝阻数学运算符的运算符重载,除非类本身在逻辑上被认为是一个数字。 – selbie
+1 to @selbie:在大多数情况下,操作符重载只会掩盖代码的逻辑。 –