-2

I have always thought that c arrays would be faster than std::array in C++ but I have done some benchmarking based off the accessing speeds and it appears that the std::array is faster. Why is that?

11
  • 1
    @R.MartinhoFernandes Not necessarily. An std::array can, for example, provide bounds checking. So can a C style array, for that matter, but it's a lot more difficult, and considerably less likely. Commented Jan 29, 2013 at 13:47
  • Perhaps to reverse the negative votes, you might want to post what those benchmark tests were. You've made the question a bit vague. What were the circumstances that the std:array was faster? Commented Jan 29, 2013 at 13:53
  • @JamesKanze this reference site en.cppreference.com/w/cpp/container/array/operator_at states that array::operator[] does not do any bounds checking. Do you have a quote in the Standard that says it can? Commented Jan 29, 2013 at 13:56
  • 1
    @rhalbersma: think debug builds, where the implementation may choose to assert(index < size()). It's undefined to access the indices past the array anyway, so the implementation is free to do whatever it wants if you violate this. Commented Jan 29, 2013 at 14:05
  • 2
    std::array holds a regular C array internally. While it should be quite easy for the compiler to yield the same performance as that of the underlying type (inlining functions) I very much doubt that the performance can be better than the underlying type. Commented Jan 29, 2013 at 14:46

1 Answer 1

5

The answer is "it depends", or perhaps better put as "nobody knows", as this type of question is always tightly coupled with compiler optimisations, processor architecture and many other factors.

I would also like to point out that if you find one better on one system, it may not reflect that it's better in a diffferent situation - e.g. different compiler or different processor or different operating system. If the overall solution is very similar [and I believe they are in this case], the results of small differences in code generation, or differences in processor architecture can alter the results in either direction. Or for example how well memory allocations line up with cache-lines or virtual memory pages to avoid "extra work".

ALWAYS when discussing performance, never guess and don't ask on the internet. Measure all the options, measure all options again (on some other platform, different compiler options, etc), and then decide which is better [assuming the code is at all performance critical in first place].

Sign up to request clarification or add additional context in comments.

3 Comments

Add "It depends" to beginning of your answer and it may be answer as well ;)
@Mats Petersson You make very good points I probably should have thought about. Thank you. :)
I've altered my answer to cover "why it is important to measure" and "why nobody knows".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.