std::array vs array performance
C++C++11StdarrayC++ Problem Overview
If I want to build a very simple array like:
int myArray[3] = {1,2,3};
Should I use std::array
instead?
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?
C++ Solutions
Solution 1 - C++
> What are the advantages of using std::array
over usual ones?
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.
> 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.
Solution 2 - C++
A std::array
is a very thin wrapper around a C-style array, basically defined as
template<typename T, size_t N>
struct array
{
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.
Solution 3 - C++
std::array
is designed as zero-overhead wrapper for C arrays that gives it the "normal" value like semantics of the other C++ containers.
You should not notice any difference in runtime performance while you still get to enjoy the extra features.
Using std::array
instead of int[]
style arrays is a good idea if you have C++11 or boost at hand.
Solution 4 - C++
> > 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::array
does not always produce identical assembly code compared to C-array depending on the specific platform.
I tested this specific situation on 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];
}
}
GCC and Clang produce identical assembly code for both the C-array version and the std::array
version.
MSVC and ICPC, however, produce different assembly code for each array version. (I tested ICPC19 with -Ofast
and -Os
; MSVC -Ox
and -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.
As a little extra: There seems to be a bug in ICPC with
#pragma simd
for vectorization when using the c-array in some situations
(the c-array code produces a wrong output; the std::array
version works fine).
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::array
and #pragma simd
.
Solution 5 - C++
std::array
has value semantics while raw arrays do not. This means you can copy std::array
and treat it like a primitive value. You can receive them by value or reference as function arguments and you can return them by value.
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::array
will do the right thing and should still give equal performance.
Solution 6 - C++
You will get the same perfomance results using std::array
and c array
If you run these code:
std::array<QPair<int, int>, 9> *m_array=new std::array<QPair<int, int>, 9>();
QPair<int, int> *carr=new QPair<int, int>[10];
QElapsedTimer timer;
timer.start();
for (int j=0; j<1000000000; j++)
{
for (int i=0; i<9; i++)
{
m_array->operator[](i).first=i+j;
m_array->operator[](i).second=j-i;
}
}
qDebug() << "std::array<QPair<int, int>" << timer.elapsed() << "milliseconds";
timer.start();
for (int j=0; j<1000000000; j++)
{
for (int i=0; i<9; i++)
{
carr[i].first=i+j;
carr[i].second=j-i;
}
}
qDebug() << "QPair<int, int> took" << timer.elapsed() << "milliseconds";
return 0;
You will get these results:
std::array<QPair<int, int> 5670 milliseconds
QPair<int, int> took 5638 milliseconds
Mike Seymour is right, if you can use std::array
you should use it.