LoginSignup
1
0

More than 5 years have passed since last update.

mat2cell vs forループ

Posted at

mat2cell駆使してたらえらい遅いことに気がついたので,簡単な実験をしてみた.
ランダムな正数の入ったN*1の行列をN*1 個のcellに分割する(N=1e1,1e2,...,1e7).
ご指摘等ありましたらよろしくお願いします.

mat2cell-vs-for.m
t1=zeros(7,1);
t2=t1;
for n=1:7
    N=10^n;
    a=randi(N,N,1);
    % mat2cell
    tic
        out=cell(N,1);
        out=mat2cell(a,ones(N,1));
    t1(n)=toc
    %
    % for
    tic
        out=cell(N,1);
        for i=1:P
            out{i}=a(i);
        end
    t2(n)=toc
end
% visualization
plot(1:7,[t1,t2])
legend({'mat2cell','for-loop'})
set(gca,'XTickLabel',{'1e1','1e2','1e3','1e4','1e5','1e6','1e7'})
xlabel('Vector size')
ylabel('Elapsed time (sec)')

実験結果
Mac Pro (Mid 2010)
OS: 10.7
processor: 2 x 2.66GHz 6-Core Intel Xeon
mem: 16GB
Matlab 2012a

t1 =
0.0008
0.0006
0.0047
0.0437
0.4256
4.2257
43.2572

t2 =
0.0003
0.0000
0.0002
0.0014
0.0187
0.1723
1.4863

結果

コード的にはmat2cellのが断然きれいなのだけど,データのサイズが大きくなると計算時間は比較にならないようです.分割数を変えた場合や,分割サイズを可変にした場合どうなるかについては,未検証…

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0