考虑预先分配速度 [英] Consider preallocating for speed
问题描述
我正在做以下事情
for i = 1:m,
index = 0;
for j = 1:n,
index = index+values(i,j)*2^(j-1);
if (j==1)
symbol_chip = chip_values(index+1,:);
else
symbol_chip = [symbol_chip chip_values(index+1,:)];
end
end
end
它告诉我以下内容:
symbol_chip
可能在循环内部增长.考虑预先分配速度.
symbol_chip
might be growing inside the loop. Consider preallocating for speed.
有什么想法吗?
推荐答案
是.每次走动时,您的elseif
块都会调整symbol_chip
的大小,这很昂贵.而是,重写代码,以便在循环之前使用(例如)symbol_chip = zeros(max_size, 1);
.然后,更改内容,但不要更改symbol_chip
的大小.
Yes. Each time you go around, your elseif
block is resizing symbol_chip
, which is expensive. Instead, rewrite your code so that you have (say) symbol_chip = zeros(max_size, 1);
before the loop. Then, change the contents but not the size of symbol_chip
.
您需要稍作更改,但是这样做会更快.如果您对当前的速度不满意,请不要进行任何更改!
You'll need to slightly change your approach, but it will be much faster if you do. If you're not annoyed by the current speed, don't change anything!
这篇关于考虑预先分配速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!