调整大数组 [英] Resizing big arrays

查看:115
本文介绍了调整大数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组的大小而像2 GB(装有音频样本)。现在,我想申请一个过滤器数组。该过滤器产生喜欢比输入信号源多出50%的样品。所以现在我需要创建新的数组,大小为3 GB。现在,我给了5 GB的内存使用。但是,如果该过滤器可以在那个源阵列只操作,只需要在这个数组中的一些更多的空间。
问:我可以分配在C#中的内存,可以调整W / O创建第二个内存块,然后去除第一个?
我只是想,如果在电脑的内存被分为4 KB页(或更多),那么,为什么C#不(?)的使用,良好的功能?

I have an array which size is like 2 GB (filled with audio samples). Now I want to apply a filter for that array. This filter is generating like 50% more samples than input source. So now I need to create new array which size is 3 GB. Now I gave 5 GB of memory used. But if this filter can operate only at that source array and only need some more space in this array. Question: can I allocate a memory in C# that can be resized w/o creating a second memory block, then removing that first one? I just thought, If memory in PC's is divided into 4 kB pages (or more), so why C# cannot (?) use that good feature?

推荐答案

如果您的过滤器可以在工作的地方在一开始只是分配更多的50%的空间。所有你需要知道的是原始样品的实际长度。

If your filter can work in-place just allocate 50% more space at the beginning. All you need to know is the actual length of the original sample.

如果是code并不总是工作,你不想提前消耗更多的内存,可以分配原来的一半阵列(扩展阵列),并检查您的访问涉及哪个部分:

If that code doesn't work always and you don't want to consume more memory beforehand, you can allocate half of the original array (the extension array) and check which part your access relates to:

byte[] myOriginalArray = new byte[2GB]; // previously allocated 

byte[] myExtensionArray = new byte[1GB]; // 50% of the original
for(... my processing code of the array ...)
{
  byte value = read(index);
  ... process the index and the value here
  store(index, value);
}

byte read(int index)
{
  if(index < 2GB) return myOriginalArray[index];
  return myExtensionArray[index - 2GB];
}

void store(int index, byte value)
{
   if(index < 2GB) myOriginalArray[index] = value;
   myExtensionArray[index - 2GB] = value;
}

您添加索引检查和减法开销为每个访问阵列。这也可以对某些案件做出更聪明。例如对于部分不需要访问扩展您可以使用更快的环路,并在您需要写扩展部分的零件可以使用较慢的版本(两个连续的循环)。

You add index check and subtraction overhead for each access to the array. That could also be made smarter for certain cases. For instance for the portion you do not need to access extension you can use your faster loop and for the part where you need to write to extension part you can use the slower version (two consecutive loops).

这篇关于调整大数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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