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

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

问题描述

我刚开始在一个科学项目中,速度真的很重要(HPC)工作。我目前正在设计数据structes。该项目的核心是双精度值的三维网格,以解决部分differenital方程。

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.

由于这里的速度是一个可能更大的担忧则code的简单,我想知道的STL相比于通常的C风格的数组如何执行。对我来说,因为它是一个三维网格,我是用线性索引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.

我抬头一看年长的问题,但我只找到concering建筑/销毁的问题(这并不重要,因为数据结构是在程序开始只创建一次 - 快速索引和计算它是很重要的),或者不同的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 ::矢量此平向量可能会更
比使用简单的指针存储器昂贵(或者它可以
不是选项,您可以事先不知道)。我还是会去的
矢量首先,在控制课堂上认真包装的一切,
如果仍存在性能问题,切换到
指针。

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天全站免登陆