C++ std::array 与数组性能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30263303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:44:39  来源:igfitidea点击:

std::array vs array performance

c++c++11stdarray

提问by Arcyno

If I want to build a very simple array like

如果我想构建一个非常简单的数组,例如

int myArray[3] = {1,2,3};

Should I use std::arrayinstead ?

我应该std::array改用吗?

std::array<int, 3> a = {{1, 2, 3}};

What are the advantages of using std::array over usual ones? Is it more performant ? Just easier to handle for copy/access ?

使用 std::array 比通常的有什么优点?它的性能更高吗?只是更容易处理复制/访问?

回答by Mike Seymour

What are the advantages of using std::arrayover usual ones?

使用std::array比通常的有什么优势?

It has friendly value semantics, so that it can be passed to or returned from functions by value. Its interface makes it more convenient to find the size, and use with STL-style iterator-based algorithms.

它具有友好的值语义,因此可以按值传递给函数或从函数返回。它的界面使得查找大小和使用基于 STL 风格的迭代器算法变得更加方便。

Is it more performant ?

它的性能更高吗?

It should be exactly the same. By definition, it's a simple aggregate containing an array as its only member.

它应该完全一样。根据定义,它是一个包含数组作为其唯一成员的简单聚合。

Just easier to handle for copy/access ?

只是更容易处理复制/访问?

Yes.

是的。

回答by vsoftco

A std::arrayis a very thin wrapper around a C-style array, basically defined as

Astd::array是围绕 C 风格数组的一个非常薄的包装器,基本上定义为

template<typename T, size_t N>
class array
{
public:
    T _data[N];
    T& operator[](size_t);
    const T& operator[](size_t) const;
    // other member functions and typedefs
};

It is an aggregate, and it allows you to use it almost like a fundamental type (i.e. you can pass-by-value, assign etc, whereas a standard C array cannot be assigned or copied directly to another array). You should take a look at some standard implementation (jump to definition from your favourite IDE or directly open <array>), it is a piece of the C++ standard library that is quite easy to read and understand.

它是一个聚合,它允许您几乎像基本类型一样使用它(即您可以按值传递、赋值等,而标准 C 数组不能直接赋值或复制到另一个数组)。您应该查看一些标准实现(从您喜欢的 IDE 跳转到定义或直接打开<array>),它是 C++ 标准库的一部分,非常易于阅读和理解。

回答by Baum mit Augen

std::arrayis designed as zero-overhead wrapper for C arrays that gives it the "normal" value like semantics of the other C++ containers.

std::array被设计为 C 数组的零开销包装器,赋予它“正常”值,就像其他 C++ 容器的语义一样。

You should not notice any difference in runtime performance while you still get to enjoy the extra features.

在您仍然可以享受额外功能的同时,您应该不会注意到运行时性能的任何差异。

Using std::arrayinstead of int[]style arrays is a good idea if you have C++11 or boost at hand.

如果您手头有 C++11 或 boost,使用std::array代替int[]样式数组是一个好主意。

回答by Henryk

Is it more performant ?

It should be exactly the same. By definition, it's a simple aggregate containing an array as its only member.

它的性能更高吗?

它应该完全一样。根据定义,它是一个包含数组作为其唯一成员的简单聚合。

The situation seems to be more complicated, as std::arraydoes not always produce identical assembly code compared to C-array depending on the specific platform.

情况似乎更复杂,因为std::array与 C 数组相比,根据特定平台并不总是生成相同的汇编代码。

I tested this specific situation on godbolt:

我在Godbolt上测试了这种特定情况:

#include <array>
void test(double* const C, const double* const A,
          const double* const B, const size_t size) {
  for (size_t i = 0; i < size; i++) {
    //double arr[2] = {0.e0};//
    std::array<double, 2> arr = {0.e0};//different to double arr[2] for some compiler
    for (size_t j = 0; j < size; j++) {
      arr[0] += A[i] * B[j];
      arr[1] += A[j] * B[i];
    }
    C[i] += arr[0];
    C[i] += arr[1];
  }
}

GCCand Clangproduce identical assembly code for both the C-array version and the std::arrayversion.

GCCClang为 C 数组版本和std::array版本生成相同的汇编代码。

MSVCand ICPC, however, produce different assembly code for each array version. (I tested ICPC19 with -Ofastand -Os; MSVC -Oxand -Os)

但是,MSVCICPC为每个数组版本生成不同的汇编代码。(我用-Ofast-Os; MSVC-Ox和测试了ICPC19 -Os

I have no idea, why this is the case (I would indeed expect exactly identical behavior of std::array and c-array). Maybe there are different optimization strategies employed.

我不知道为什么会这样(我确实希望 std::array 和 c-array 的行为完全相同)。也许采用了不同的优化策略。

As a little extra: There seems to be a bug in ICPC with

补充一点:ICPC 中似乎存在一个错误

#pragma simd 

for vectorization when using the c-array in some situations (the c-array code produces a wrong output; the std::arrayversion works fine).

在某些情况下使用 c-array 时进行矢量化(c-array 代码产生错误的输出;std::array版本工作正常)。

Unfortunately, I do not have a minimal working example for that yet, since I discovered that problem while optimizing a quite complicated piece of code.

不幸的是,我还没有一个最小的工作示例,因为我在优化一段相当复杂的代码时发现了这个问题。

I will file a bug-report to intel when I am sure that I did not just misunderstood something about C-array/std::arrayand #pragma simd.

当我确定我没有误解 C-array/std::array#pragma simd.

回答by b4hand

std::arrayhas value semantics while raw arrays do not. This means you can copy std::arrayand treat it like a primitive value. You can receive them by value or reference as function arguments and you can return them by value.

std::array具有值语义,而原始数组没有。这意味着您可以std::array像原始值一样复制和处理它。您可以按值或作为函数参数的引用接收它们,也可以按值返回它们。

If you never copy a std::array, then there is no performance difference than a raw array. If you do need to make copies then std::arraywill do the right thing and should still give equal performance.

如果您从不复制 a std::array,则与原始数组没有性能差异。如果您确实需要制作副本,那么std::array将做正确的事情并且仍然应该提供相同的性能。