STL容器速度对阵列 [英] STL containers speed vs. arrays

查看:145
本文介绍了STL容器速度对阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始一个科学项目,速度真的很重要(HPC)。我目前正在设计数据结构。该项目的核心是一个双值的3D网格,以求解部分差分方程。

I just started working on a scientific project where speed really matters (HPC). I'm currently designing the data structes. The core of the project is a 3D-Grid of double values, in order to solve a partial differenital equation.

由于速度这里是一个可能更大的问题,然而简单的代码,我想知道STL如何执行与通常的C风格的数组相比。在我的情况下,由于它是一个3D网格,我在想a)一维向量与线性索引b)3向量的向量或c)一维c型数组或d)三维c型数组。

Since speed here is a probably bigger concern then simplicity of the code, I'd like to know how the STL performs compared to usual C-style arrays. In my case, since it's a 3D-grid, I was thinking of a) a one dimensional vector with linear indexing b) a vector of 3 vectors or c) a one dimensional c-style array or d) a three dimensional c-style array.

我查找了较老的问题,但我只发现了问题一致的构建/销毁(这在这里不重要,因为数据结构只在程序启动时创建一次 - 快速索引和计算是重要的)或比较不同的STL容器。

I looked up older questions, but I only found questions concering construction/destruction (which is not important here, as the datastructures are only created once at program start - fast indexing and computations on it are important) or comparison of different STL containers.

感谢您的帮助

推荐答案

很难(甚至不可能)提前说出
相对表现会是什么。通常,在现代机器上,
使用平面向量,并计算其中的索引,
优于向量向量的向量;在旧机器上,
反向是真的。 (在现代机器上,乘法是
通常比由于局部性差而造成的页错过。在
旧机器上,乘法是昂贵的,并且没有
高速缓存,所以局部性没有)

It's hard (or even impossible) to say in advance what the relative performances will be. Generally, on modern machines, using a flat vector, and calculating the indexes into it, will outperform a vector of vector of vector; on older machines, the reverse was true. (On modern machines, multiplication is usually cheaper than the page misses due to poor locality. On older machines, multiplication was expensive, and there weren't caches, so locality didn't make a difference.)

此外,根据机器和库的实现,
使用 std :: vector 对于这个平面向量可能比使用一个简单的指针到内存更昂贵
(或者
不是—你不能提前知道)。我仍然先去
向量,包装一切仔细在一个控制类,
如果仍然有性能问题,切换到
指针。

Also, depending on the machine and the library implementation, using an std::vector for this flat vector may be more expensive than using a simple pointer to the memory (or it may not be—you can't know in advance). I'd still go for the vector first, wrap everything carefully in a controlling class, and if there were still performance problems, switch to the pointer.

这篇关于STL容器速度对阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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