arrays - MATLAB 矩阵预分配比动态矩阵扩展慢

标签 arrays memory matlab performance memory-management

在循环的每次迭代中,我都在计算一个 MATLAB 矩阵。这些矩阵都必须连接在一起以创建一个最终矩阵。我在进入循环之前知道这个最终矩阵的维度,所以我虽然使用“零”函数预分配矩阵比初始化一个空数组然后简单地在循环的每次迭代中附加子数组要快。奇怪的是,当我预分配时,我的程序运行速度要慢得多。这是代码(只有第一行和最后一行不同):

这很慢:

w_cuda = zeros(w_rows, w_cols, f_cols);

for j=0:num_groups-1

    % gets # of rows & cols in W. The last group is a special
    % case because it may have fewer than max_row_size rows
    if (j == num_groups-1 && mod(w_rows, max_row_size) ~= 0)
        num_rows_sub = w_rows - (max_row_size * j);    
    else
        num_rows_sub = max_row_size;
    end;

    % calculate correct W and f matrices
    start_index = (max_row_size * j) + 1;
    end_index = start_index + num_rows_sub - 1;

    w_sub = W(start_index:end_index,:);
    f_sub = filterBank(start_index:end_index,:);

    % Obtain sub-matrix
    w_cuda_sub = nopack_cu(w_sub,f_sub);

    % Incorporate sub-matrix into final matrix
    w_cuda(start_index:end_index,:,:) = w_cuda_sub;

end

这很快:

w_cuda = [];

for j=0:num_groups-1

    % gets # of rows & cols in W. The last group is a special
    % case because it may have fewer than max_row_size rows
    if (j == num_groups-1 && mod(w_rows, max_row_size) ~= 0)
        num_rows_sub = w_rows - (max_row_size * j);    
    else
        num_rows_sub = max_row_size;
    end;

    % calculate correct W and f matrices
    start_index = (max_row_size * j) + 1;
    end_index = start_index + num_rows_sub - 1;

    w_sub = W(start_index:end_index,:);
    f_sub = filterBank(start_index:end_index,:);

    % Obtain sub-matrix
    w_cuda_sub = nopack_cu(w_sub,f_sub);

    % Incorporate sub-matrix into final matrix
    w_cuda = [w_cuda; w_cuda_sub];

end

就其他可能有用的信息而言——我的矩阵是 3D 的,其中的数字很复杂。与往常一样,我们感谢您的任何见解。

最佳答案

我一直认为任何数组大小的预分配都更快,但从未实际测试过。因此,我通过追加和预分配方法使用 1000 次迭代,对从 1x1x3 到 20x20x3 的各种数组大小的填充进行了简单的测试。代码如下:

arraySize = 1:20;
numIteration = 1000;

timeAppend = zeros(length(arraySize), 1);
timePreAllocate = zeros(length(arraySize), 1);

for ii = 1:length(arraySize); 
    w = [];
    tic;
    for jj = 1:numIteration
        w = [w; rand(arraySize(ii), arraySize(ii), 3)];
    end
    timeAppend(ii) = toc;
end; 

for ii = 1:length(arraySize); 
    w = zeros(arraySize(ii) * numIteration, arraySize(ii), 3);
    tic;
    for jj = 1:numIteration
        indexStart = (jj - 1) * arraySize(ii) + 1;
        indexStop = indexStart + arraySize(ii) - 1;
        w(indexStart:indexStop,:,:) = rand(arraySize(ii), arraySize(ii), 3);
    end
    timePreAllocate(ii) = toc;
end; 

figure;
axes;
plot(timeAppend);
hold on;
plot(timePreAllocate, 'r');
legend('Append', 'Preallocate');

以下是(如预期的)结果: Comparison of array appending vs. preallocation

关于arrays - MATLAB 矩阵预分配比动态矩阵扩展慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5171356/

相关文章:

c++ - 在 C/C++ 中为数组参数传递 NULL

javascript - 使用javascript删除数组元素

c - gdb 检查和打印给出不同的值

c - C 中 strncpy 的内存混淆

matlab:在 500x500 像素 .jpg 图像中找到绿色边缘的 x-y 位置

c - 在c中访问数组的-1元素

c - 字符串比较差异(适用于某些情况,有时无效)

C - 简单斐波那契程序中的共享内存

matlab - 如何将绘图保存到 PDF 文件中而没有大的边距

c - 为什么 mex 代码运行得比 matlab 代码慢