'for'循环vs矢量化在MATLAB中 [英] 'for' loop vs vectorization in MATLAB

查看:227
本文介绍了'for'循环vs矢量化在MATLAB中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MATLAB中编写了一些东西,正如所建议的那样,我总是试图使用矢量化。但是最后这个计划很慢。所以我发现在一个地方代码是显着更快的时候使用循环(下面的例子)。

我想知道如果我误解了一些东西或做错了什么,因为在这种情况下性能很重要,而且我不想猜测矢量化或者循环是否会更快。

 %数据初始化

k = 8;
n = 2 ^ k + 1;
h = 1 /(n-1);
cw = 0.1;

iter = 10000;

uloc = zeros(n);
fploc = uloc;
uloc(2:end-1,2:end-1)= 1;
vloc = uloc;
ploc = ones(n);

uloc2 = zeros(n);
fploc2 = uloc2;
uloc2(2:end-1,2:end-1)= 1;
vloc2 = uloc2;
ploc2 = ones(n);

%%%%%%%%%%%%%%%%%%%%%%%
%矢量化版本%
%%%%%%%% %%%%%%%%%%%%%%
tic
for it = 1:iter
il = 2:4;
jl = 2:4;
fploc(il,jl)= h / 6 *( - uloc(il-1,jl-1)+ uloc(il-1,jl)...
-2 * uloc (il + 1,jl + 1)...
。(1 + 1,jl + 1) (il-1,jl)...
+ vloc(il,jl-1)-vloc(il,jl-1) +1)...
+ 2 * vloc(il + 1,jl)+ vloc(il + 1,jl + 1))...
...
+ cw * (il,jl)-ploc(il,jl)+ 4 * ploc(il,jl)...
-ploc(il + 1,jl)-ploc (IL,JL + 1));
end
toc


%%%%%%%%%%%%%%%%%%%%%%
%循环版本%
%%%%%%%%%%%%%%%%%%%%%%
tic
for it = 1:iter
for il = (il-1,j-1)+ uloc2(il-1,jl)2:4
。 (i + 1,j1)+ uloc2(i1 + 1)+ 1(i + 1) ,j1 + 1)...
...
-vloc2(il-1,jl-1)-2 * vloc2(il-1,jl)...
+ vloc2 (il,jl-1)-vloc2(il,jl + 1)...
+ 2 * vloc2(il + 1,jl)+ vloc2(il + 1,jl + 1))...
...
+ cw * h ^ 2 *( - ploc2(il-1,jl)-ploc2(il,jl-1)+ 4 * ploc2(il,jl)...
-ploc2(il + 1,jl)-ploc2(il,jl + 1));
end
end
end
toc


解决方案在过去的几年中,MATLAB的即时编译器(JIT)已经有了很大的改进。即使你是正确的,一般应该矢量化代码,根据我的经验,这只适用于某些操作和功能,也取决于你的功能处理多少数据。

要找出最好的方法,最好的办法是到分析你的MATLAB代码有和没有向量化。


I was programming something in MATLAB and, as recommended, I am always trying to use vectorization. But in the end the program was quite slow. So I found out that in one place the code is significantly faster when using loops (example below).

I would like to know if I misinterpreted something or did something wrong, because performance is important in this case, and I don't want to keep guessing if vectorization or loops are going to be faster.

% data initialization

k = 8;
n = 2^k+1;
h = 1/(n-1);
cw = 0.1;

iter = 10000;

uloc = zeros(n);
fploc = uloc;
uloc(2:end-1,2:end-1) = 1;
vloc = uloc;
ploc = ones(n);

uloc2 = zeros(n);
fploc2 = uloc2;
uloc2(2:end-1,2:end-1) = 1;
vloc2 = uloc2;
ploc2 = ones(n);

%%%%%%%%%%%%%%%%%%%%%%
% vectorized version %
%%%%%%%%%%%%%%%%%%%%%%
tic
for it=1:iter
    il=2:4;
    jl=2:4;
    fploc(il,jl) = h/6*(-uloc(il-1,jl-1) + uloc(il-1,jl)...
        -2*uloc(il,jl-1)+2*uloc(il,jl+1)...
        -uloc(il+1,jl) + uloc(il+1,jl+1)...
        ...
        -vloc(il-1,jl-1) - 2*vloc(il-1,jl)...
        +vloc(il,jl-1) - vloc(il,jl+1)...
        + 2*vloc(il+1,jl) + vloc(il+1,jl+1))...
        ...
        +cw*h^2*(-ploc(il-1,jl)-ploc(il,jl-1)+4*ploc(il,jl)...
        -ploc(il+1,jl)-ploc(il,jl+1));
end
toc


%%%%%%%%%%%%%%%%%%%%%%
%    loop version    %
%%%%%%%%%%%%%%%%%%%%%%
tic
for it=1:iter
    for il=2:4
        for jl=2:4
            fploc2(il,jl) = h/6*(-uloc2(il-1,jl-1) + uloc2(il-1,jl)...
                -2*uloc2(il,jl-1)+2*uloc2(il,jl+1)...
                -uloc2(il+1,jl) + uloc2(il+1,jl+1)...
                ...
                -vloc2(il-1,jl-1) - 2*vloc2(il-1,jl)...
                +vloc2(il,jl-1) - vloc2(il,jl+1)...
                + 2*vloc2(il+1,jl) + vloc2(il+1,jl+1))...
                ...
                +cw*h^2*(-ploc2(il-1,jl)-ploc2(il,jl-1)+4*ploc2(il,jl)...
                -ploc2(il+1,jl)-ploc2(il,jl+1));
        end
    end
end
toc

解决方案

MATLAB's just in time compiler (JIT) has been improved significantly over the last couple years. And even though you are right that one should generally vectorize code, from my experience this is only true for certain operations and functions and also depends on how much data your functions are handling.

The best way for you to find out what works best, is to profile your MATLAB code with and without vectorization.

这篇关于'for'循环vs矢量化在MATLAB中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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