で
おかげでそれは面白いですので、私は対策を取ることにした:
私はMatlabのR2016aのWindowsの(64ビット)バージョンを使用しています。
CPU:Core i5-3550 at 3.3GHz。 メモリ:8GB DDR3 1333(デュアルチャンネル)。
len = 100000000; %Number of elements in array (make it large enouth to be outsize of cache memory).
idx = zeros(len, 1, 'logical'); %Fill idx with ones.
idx(1:10:end) = 1; %Put 1 in every 10'th element of idx.
a = ones(len, 1); %Fill arrary a with ones.
disp('Measure: a(idx) = [];')
tic
a(idx) = [];
toc
a = ones(len, 1);
disp(' ');disp('Measure: a = a(~idx);')
tic
a = a(~idx);
toc
disp(' ');disp('Measure: not_idx = ~idx;')
tic
not_idx = ~idx;
toc
a = ones(len, 1);
disp(' ');disp('Measure: a = a(not_idx);')
tic
a = a(not_idx);
toc
結果:
Measure: a(idx) = [];
Elapsed time is 1.647617 seconds.
Measure: a = a(~idx);
Elapsed time is 0.732233 seconds.
Measure: not_idx = ~idx;
Elapsed time is 0.032649 seconds.
Measure: a = a(not_idx);
Elapsed time is 0.686351 seconds.
結論:
a = a(~idx)
がa(idx) = []
より約二倍高速です。 a = a(~idx)
の
- 合計時間は、おそらく別途
~idx
を計算しているnot_idx = ~idx
プラスa = a(not_idx)
のMatlabの合計に等しいので、より多くのメモリを消費します。物理RAMがいっぱい消費しているだけ
メモリ消費メートル。
私はそれが無視できると思います(~idx
一時的です)。
- どちらのソリューションが最適化されていません。
Iは10倍高速であることが(C)で完全に最適化された実装、見積もります。
答えはおそらく 'a'と' idx'のサイズ、おそらくMatlabのバージョンによって決まります。なぜあなたは[time](https://es.mathworks.com/help/matlab/ref/timeit.html)の2つのオプションを自分で使ってみませんか? –