Help us understand the problem. What is going on with this article?

mat2cell vs forループ

More than 5 years have passed since last update.

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした