MATLAB矩阵preallocation速度比动态矩阵扩展 [英] MATLAB Matrix Preallocation Slower Than Dynamic Matrix Expansion

查看:263
本文介绍了MATLAB矩阵preallocation速度比动态矩阵扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个循环的每次迭代,我计算一个MATLAB矩阵。这些矩阵必须全部串联在一起创建一个最终的矩阵。我知道这最后的矩阵进入循环前的尺寸,所以我虽然preallocating使用零功能会比初始化一个空数组后来干脆在我的每个循环迭代追加子阵更快矩阵。奇怪的是,我的程序运行时我preallocate慢得多。这里是code(只有第一个和最后一个行不同):

In each iteration of a loop, I am calculating a MATLAB matrix. These matrices all must be concatenated together to create one final matrix. I know the dimensions of this final matrix before entering the loop, so I though preallocating the matrix using the 'zeros' function would be faster than initializing an empty array then simply appending the subarrays in each iteration of my loop. Oddly, my program runs MUCH slower when I preallocate. Here is the code (Only the first and last lines differ):

这是缓慢的:

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和在其内的数字是复杂的。与往常一样,任何见解是AP preciated。

As far as other potentially useful information--my matrix is 3D, and the numbers within it are complex. As always, any insight is appreciated.

推荐答案

我一直认为preallocation为任何数组大小更快,从来没有实际测试过。所以,我的确从1x1x3到20x20x3一个简单的测试时间各数组大小的人口使用1000次迭代双方追加和preallocation方法。这里的code:

I have always assumed preallocation is faster for any array size and never actually tested it. So, I did a simple test timing the population of various array sizes from 1x1x3 up to 20x20x3 using 1000 iterations by both appending and preallocation methods. Here's the code:

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');

和这里的(如预期)的结果:

And here are the (as expected) results:

这篇关于MATLAB矩阵preallocation速度比动态矩阵扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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