哪个有更好的表现?一个std :: array还是C数组? [英] Which has better performance? A std::array or C array?

查看:130
本文介绍了哪个有更好的表现?一个std :: array还是C数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为c数组比C ++中的std :: array要快,但是我已经根据访问速度进行了一些基准测试,似乎std :: array更快.这是为什么?

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?

推荐答案

答案是取决于",或者最好用没人知道"来表示,因为这种类型的问题总是与编译器优化,处理器体系结构紧密联系在一起和许多其他因素.

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

这篇关于哪个有更好的表现?一个std :: array还是C数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆