【发布时间】:2010-10-28 07:10:18
【问题描述】:
当数组大小非常大时,我的程序中的瓶颈是计算数组中所有数字的符号。我在下面展示了我尝试过的两种方法,它们的结果都相似。我有 16GB 的 RAM,阵列占用约 5GB。我看到的问题是符号函数占用了大量的 RAM + 虚拟内存。任何人都知道减少内存需求并加快将数组输入符号放入数组输出的过程(见下文)的方法吗?
将 for 循环与 if 或 switch 命令一起使用不会耗尽内存,但需要一个小时才能完成(太长了)。
size = 1e9; % size of large array (just an example, could be larger)
output = int8(zeros(size,1)-1); % preallocate to -1
input = single(rand(size,1)); % create random array between 0 and 1
scalar = single(0.5); % just a scalar number, set to 0.5 (midpoint) for example
% approach 1 (comment out when using approach 2)
output = int8(sign(input - scalar)); % this line of code uses a ton of RAM and virtual memory
% approach 2
output(input>scalar) = 1; % this line of code uses a ton of RAM and virtual memory
output(input==scalar) = 0; % this line of code uses a ton of RAM and virtual memory
提前感谢您的任何建议。
【问题讨论】:
-
您是否尝试过使用 MEX 文件的 C 实现?
-
您希望在实际的单精度值数组中看到什么范围的值?
-
取值范围在 +500 到 -500 之间,包括 0。
-
这些值是该范围内的整数,还是任何浮点值?
-
任何浮点值。该阵列实际上是来自示波器的模数转换器的输出,归一化为伏特单位。示波器仅接受 +/- 500V 输入,但实际上输入数组将在 +/- 50V 范围内,全是浮点数(很少是整数,但它可能会发生)。
标签: matlab