How to write vectorized functions in MATLAB

后端 未结 4 1013
时光取名叫无心
时光取名叫无心 2021-02-09 16:41

I am just learning MATLAB and I find it hard to understand the performance factors of loops vs vectorized functions.

In my previous question: Nested for

4条回答
  •  我在风中等你
    2021-02-09 17:36

    Vectorization

    In addition to whats already been said, you should also understand that vectorization involves parallelization, i.e. performing concurrent operations on data as opposed to sequential execution (think SIMD instructions), and even taking advantage of threads and multiprocessors in some cases...

    MEX-files

    Now although the "interpreted vs. compiled" point has already been argued, no one mentioned that you can extend MATLAB by writing MEX-files, which are compiled executables written in C, that can be called directly as normal function from inside MATLAB. This allows you to implement performance-critical parts using a lower-level language like C.

    Column-major order

    Finally, when trying to optimize some code, always remember that MATLAB stores matrices in column-major order. Accessing elements in that order can yield significant improvements compared to other arbitrary orders.

    For example, in your previous linked question, you were computing the median of set of stacked images along some dimension. Now the order in which those dimensions are ordered greatly affect the performance. Illustration:

    %# sequence of 10 images
    fPath = fullfile(matlabroot,'toolbox','images','imdemos');
    files = dir( fullfile(fPath,'AT3_1m4_*.tif') );
    files = strcat(fPath,{filesep},{files.name}');      %'
    
    I = imread( files{1} );
    
    %# stacked images along the 1st dimension: [numImages H W RGB]
    stack1 = zeros([numel(files) size(I) 3], class(I));
    for i=1:numel(files)
        I = imread( files{i} );
        stack1(i,:,:,:) = repmat(I, [1 1 3]);   %# grayscale to RGB
    end
    
    %# stacked images along the 4th dimension: [H W RGB numImages]
    stack4 = permute(stack1, [2 3 4 1]);
    
    %# compute median image from each of these two stacks
    tic, m1 = squeeze( median(stack1,1) ); toc
    tic, m4 = median(stack4,4); toc
    isequal(m1,m4)
    

    The timing difference was huge:

    Elapsed time is 0.257551 seconds.     %# stack1
    Elapsed time is 17.405075 seconds.    %# stack4
    

提交回复
热议问题