Which has better performance? A std::array or C array? [closed]

混江龙づ霸主 提交于 2019-12-25 18:54:06

问题


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?


回答1:


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].



来源:https://stackoverflow.com/questions/14584367/which-has-better-performance-a-stdarray-or-c-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!