对矢量进行算术运算

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中受益。这可能是免费的表演++。