如何加快c++中二维三角矩阵的内存分配?

发布于 2024-10-04 03:39:35 字数 433 浏览 7 评论 0原文

我需要为代表三角矩阵的非常大的数组分配内存。 我编写了以下代码:

const int max_number_of_particles=20000;
float **dis_vec;

dis_vec = new float **[max_number_of_particles];

for (i = 0; i<max_number_of_particles; i++)
  dis_vec[i] = new float *[i];

for (i = 0; i<max_number_of_particles; i++)
  for (j = 0; j<i; j++)
    dis_vec[i][j] = new float[2];

问题是,随着矩阵大小的增加,执行此操作(分配内存)所需的时间迅速增加。有谁知道这个问题的更好解决方案?

谢谢。

I need to allocate memory for a very large array which represents triangular matrix.
I wrote the following code:

const int max_number_of_particles=20000;
float **dis_vec;

dis_vec = new float **[max_number_of_particles];

for (i = 0; i<max_number_of_particles; i++)
  dis_vec[i] = new float *[i];

for (i = 0; i<max_number_of_particles; i++)
  for (j = 0; j<i; j++)
    dis_vec[i][j] = new float[2];

The problem is that the time needed to do it (to allocate the memory) quickly increases with the increasing size of matrix. Does anyone know better solution for this problem?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

榆西 2024-10-11 03:39:35

分配一维数组并将索引转换为下标,反之亦然。与 O(N) 分配相比,一次分配应该快得多。

编辑

具体来说,只需分配N(N+1)/2个元素,当您想要访问[r][c]时原来,只需访问 [r*(r+1)/2 + c] 即可。

Allocate a one dimensional array and convert indices to subscripts and vice versa. One allocation compared to O(N) allocations should be much faster.

EDIT

Specifically, just allocate N(N+1)/2 elements, and when you want to access [r][c] in the original, just access [r*(r+1)/2 + c] instead.

陈独秀 2024-10-11 03:39:35

是的。

首先......从你的内部循环开始。

“new float[2]”

分配一个数组,我想分配一个数组比分配一个恰好有 2 个浮点数的固定大小对象要慢。

结构Float2D {
浮动一个;
浮动b;
};

x = 新的 Float2D;

这看起来更好。

但实际上,忘记这一切吧。如果你想要快速...只需 malloc 一堆浮点数。

我想说...让一些花车浪费掉。只需分配一个普通的旧二维数组即可。

float* f = (float*)malloc( max_number_of_articles*max_number_of_articles*2*sizeof(float) );

唯一可以解决这个问题的尺寸节省是通过使用三角形而不是正方形来节省 2 倍的尺寸。

然而,我非常确定你已经通过使用“new float[2]”和“new float *[i];”杀死了整个“大小节省”。我不确定“new”的开销是多少,但我想它就像 malloc 除了更糟之外。我认为大多数 malloc 每次分配大约有 8 个字节的开销。

所以你已经拥有的比分配一个正方形而损失 2 倍的大小更糟糕。

此外,它使数学变得更简单。您需要进行一些奇怪的“三角数”数学运算才能获得指针。类似 (n+1)*n/2 或其他任何东西:)

Yes.

First... start with your inner loop.

"new float[2]"

That allocates an array, which I imagine is slower to allocate than a fixed size object that happens to have 2 floats.

struct Float2D {
float a;
float b;
};

x = new Float2D;

that seems better.

But really, forget all that. If you want it fast... just malloc a bunch of floats.

I'd say... let some floats go to waste. Just alloc a plain old 2D array.

float* f = (float*)malloc( max_number_of_particles*max_number_of_particles*2*sizeof(float) );

The only size saving you could get over this, is a 2x size saving by using a triangle instead of a square.

However, I'm pretty damn sure you KILLED that entire "size saving" already, by using "new float[2]", and "new float *[i];". I'm not sure how much the overhead of "new" is, but I imagine it's like malloc except worse. And I think most mallocs have about 8 bytes overhead per allocation.

So what you have already is WORSE than a 2X size lost by allocating a square.

Also, it makes the math simpler. You'd need to do some wierd "Triangular number" math to get the pointer. Something like (n+1)*n/2 or whatever it is :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文