对矢量进行算术运算
Arithmetic operations on vectors
本文关键字:算术运算 更新时间:2023-10-16
假设我想为std::vector
提供逐元素算术运算operator+=
和operator+
,以便逐元素添加矢量条目。通常,我看到operator+
是根据operator+=
来实现的,就像这样:
#include <algorithm>
#include <vector>
template<class Type>
std::vector<Type> & operator+=(std::vector<Type> &x, const std::vector<Type> &y) {
// Checks for equal size of x and y omitted here...
std::transform(std::begin(x), std::end(x), std::begin(y), std::begin(x), std::plus<Type>());
return x;
}
template<class Type>
std::vector<Type> operator+(std::vector<Type> x, const std::vector<Type> &y) {
// Checks for equal size of x and y omitted here...
return x += y;
}
int main() {
std::vector<double> v0{1.0, 2.0, 3.0};
auto v1 = v0;
auto v2 = v0;
v2 += v0; // yields [2, 4, 6]
auto v3 = v0 + v1; // yields [2, 4, 6]
return 0;
}
就性能而言,我想
template<class Type>
std::vector<Type> operator+(const std::vector<Type> &x, const std::vector<Type> &y) {
// Checks for equal size of x and y omitted here...
std::vector<Type> result;
result.reserve(x.size());
std::transform(std::begin(x), std::end(x), std::begin(y), std::back_inserter(result), std::plus<Type>());
return result;
}
更有效,因为它避免了在输入函数时初始化第一个参数的副本,而是将结果直接放入未初始化的内存块中。实现第二个版本真的值得吗?或者我可以假设编译器无论如何都要优化吗?此外,我认为第二种备选方案比第一种方案更不通用。想象一下类似的东西
#include <array>
#include <type_traits>
template<class Container, class Enable = void>
struct IsSequenceContainer: public std::false_type {
};
template<>
template<class Type, std::size_t size>
struct IsSequenceContainer<std::array<Type, size> >: public std::true_type {
};
template<>
template<class Type, class Allocator>
struct IsSequenceContainer<std::vector<Type, Allocator> >: public std::true_type {
};
// Use the following operations for std::array and std::vector
template<class Container>
typename std::enable_if<IsSequenceContainer<Container>::value, Container>::type operator+(Container x, const Container &y) {
return x += y;
}
与性能相关的一切一样:评测程序,看看会发生什么
我的猜测是,编译器不会完全优化代码,而且这可能永远都不会有意义。唯一能确定的方法就是尝试一下。
根据+=
实现+
具有已知这两个操作是等效的优点。这样就不太可能出现错误。在你放弃这个优势之前,你应该确保你的优化是必要的。C++的习惯用法通常成为习惯用法是有充分理由的。
您看过std::valarray
吗?它已经提供了您需要的操作,您可能会从SIMD中受益。这可能是免费的表演++。
相关文章:
- 算术运算的结果类似于:C浮点变量中的1/3
- 更快的C++算术运算
- 为什么循环体中的一个基本算术运算执行得比两个算术运算慢
- 为什么对无符号字符进行算术运算会将它们提升为有符号整数
- 如何在 C++ 中计算字符串的算术运算
- 复数的算术运算
- 具有非常非常大数字的算术运算
- 如何使用C++中的处理器指令来实现快速算术运算
- boost::spirit::x3 phrase_parse 在进入 Vector 之前进行算术运算
- C++中混合数据类型的算术运算
- C++:编译器是否优化整数 + 浮点算术运算?
- 如何在特征中对二维数组的列执行简单的算术运算
- 使用数字列表和算术运算获取目标数字
- C++:“enable_if”用于限制支持特定算术运算的类型
- 如何正确避免 SIGFPE 和算术运算溢出
- 编译器用于编译 128 位整数的基本算术运算的技巧
- 将临时值存储为某种数据类型时,算术运算的标准规则是什么
- 相同的算术运算在 C++ 和 Python 中给出不同的结果
- 带有空的算术运算
- 算术运算和编译器优化