我的程序中的瓶颈是计算数组中所有数字的数字符号,当数组大小非常大时.我展示了我在下面尝试过的两种方法,两种方法都有类似的结果.我有16GB的RAM,阵列占用约5GB.我看到的问题是签名功能占用了大量的RAM虚拟内存.任何人都知道一种方法来减少内存需求并加快将数组输入的符号放入数组输出的过程(见下文)?
使用if或switch命令的for循环不会耗尽内存,但需要一个小时才能完成(方式太长).
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
在此先感谢您的任何建议.