内存布局:2D N * M的数据作为指针N * M缓冲区或为N指针数组的数组 [英] Memory layout : 2D N*M data as pointer to N*M buffer or as array of N pointers to arrays

查看:121
本文介绍了内存布局:2D N * M的数据作为指针N * M缓冲区或为N指针数组的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在犹豫如何安排我的二维数据的内存布局。
基本上,我要的是一个N * M的2D 双击阵列,其中N〜M为数以千计(从用户提供的数据导出)

I'm hesitating on how to organize the memory layout of my 2D data. Basically, what I want is an N*M 2D double array, where N ~ M are in the thousands (and are derived from user-supplied data)

我看到它的方式,我有2个选择:

The way I see it, I have 2 choices :

double *data = new double[N*M];

double **data = new double*[N];
for (size_t i = 0; i < N; ++i)
     data[i] = new double[M];

第一个选择是什么,我一边倒。
我看到的主要优点是缩短新/删除语法,连续的内存布局意味着在运行时相邻的存储访问,如果我正确地安排我的访问,以及矢量code(自动矢量化或使用矢量库如VDSP的可能是更好的性能或vecLib)

The first choice is what I'm leaning to. The main advantages I see are shorter new/delete syntax, continuous memory layout implies adjacent memory access at runtime if I arrange my access correctly, and possibly better performance for vectorized code (auto-vectorized or use of vector libraries such as vDSP or vecLib)

在另一方面,它在我看来,分配的​​连续存储可能会失败/带相比,分配了一堆规模较小的更多的时间一大块。第二种方法也具有更短的语法的优势数据[I] [J] 相比,数据[I * M + J]

On the other hand, it seems to me that allocating a big chunk of continuous memory could fail/take more time compared to allocating a bunch of smaller ones. And the second method also has the advantage of the shorter syntax data[i][j] compared to data[i*M+j]

什么是最常见的/更好的方式来做到这一点,主要是如果我尝试从性能的角度查看它(即使它们是会是小的改进,我很好奇,想看看这将更多的表演)。

What would be the most common / better way to do this, mainly if I try to view it from a performance standpoint (even though those are gonna be small improvements, I'm curious to see which would more performing).

推荐答案

前两个选择之间,为 M N <合理值/ code>,我几乎可以肯定的选择1.去你跳过一个指针引用,如果你按照正确的顺序访问数据,你会得到很好的缓存。

Between the first two choices, for reasonable values of M and N, I would almost certainly go with choice 1. You skip a pointer dereference, and you get nice caching if you access data in the right order.

在你的左右大小的担忧方面,我们可以做一些反向的最粗略计算。

In terms of your concerns about size, we can do some back-of-the-envelope calculations.

由于 M N 都在千元,假设每个为 10000 作为上限。那么你消耗的总内存为

Since M and N are in the thousands, suppose each is 10000 as an upper bound. Then your total memory consumed is

 10000 * 10000 * sizeof(double) = 8 * 10^8

这是大约800 MB,而其中大,是比较合理的给出内存在现代机器的大小。

This is roughly 800 MB, which while large, is quite reasonable given the size of memory in modern day machines.

这篇关于内存布局:2D N * M的数据作为指针N * M缓冲区或为N指针数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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