嵌套向量与连续数组对性能的影响 [英] Performance Impact of Nested Vectors vs. Contiguous Arrays

查看:78
本文介绍了嵌套向量与连续数组对性能的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何可靠的测试可以清楚地显示出访问嵌套向量和写入嵌套向量与C ++内置数组之间的性能差异?我听说使用嵌套(多维)向量与访问单个数组中的元素(所有元素都存储在连续内存中)相比通常具有一些性能开销,但这对我来说似乎只是假设.我还没有看到任何实际显示出这些差异的测试.它们重要吗?我确定这取决于场景,但是作为一个没有经验的程序员,我不确定这些差异在什么级别上变得显着.

Have there been any reliable tests that clearly display the performance differences between accessing and writing to nested vectors versus C++'s built-in arrays? I've heard that using nested (multi-dimensional) vectors typically have some performance overhead as compared to accessing elements in a single array (where all elements are stored in contiguous memory), but this just all seems to be hypothetical to me. I have yet to see any tests that actually show these differences. Are they significant? I'm sure that it depends on the scenario, but as an inexperienced programmer, I'm not quite sure at what level these differences do become significant.

推荐答案

在某种程度上,它绝对取决于场景,我认为不可能以一般方式回答哪种方法最快.最快的方法将是访问模式具有最佳数据局部性的方法-这在很大程度上取决于访问模式以及结构在内存中的布局方式,在嵌套向量的情况下,这取决于分配器可能在编译器之间有很大的差异.

It definitely depends on the scenario, to the extent that I don't think it's possible to answer in a general way which approach is fastest. The fastest approach is going to be the one where the access patterns have the best data locality - which depends highly on the access pattern as well as how the structures are laid out in memory, which in the case of nested vectors is dependent on the allocator and probably varies quite a bit between compilers.

我会遵循优化的一般规则,即首先以最简单的方式编写东西,然后在可以证明存在瓶颈的情况下尝试进行优化.

I'd follow the general rule of optimization, which is to first write things in the most straightforward way and then attempt optimization when you can prove there is a bottleneck.

这篇关于嵌套向量与连续数组对性能的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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