フィルターのクリア

Which of these two assignments is more efficient

2 ビュー (過去 30 日間)
bethel o
bethel o 2016 年 11 月 15 日
コメント済み: Jan 2016 年 11 月 16 日
Please have a look at the function below and let me know which of the sections labelled (1) and (2) is more efficient.
function pw =realtime_func(c)
persistent c_mem;
if isempty(c_mem)
c_mem=zeros(30,1);
end
%%(1) Is this faster
c_mem=[c_mem(2:end);c];
%%(2) or is this faster
c_mem(1)=[];
c_mem(end+1)=c;
% output
pw=hasTrumpCrashedTheEconomyYet(c_mem);
end
  1 件のコメント
KSSV
KSSV 2016 年 11 月 15 日
You can run a profiler to check your self or you can use tic, toc to check the timings. By the way is your label (2) working? It will throw error.
Give details, what is c and what you want to do.

サインインしてコメントする。

採用された回答

Jan
Jan 2016 年 11 月 15 日
Most likely method 1 is faster, because 2 must allocate 2 arrays. But Matlab's JIT acceleration might recognize this and optimize the code. So the only reliable answer is to measure it. Use timeit or run a tic toc and a loop with perhaps 1 million iterations.
Note that asking in the forum needs some time also. So you will have to run the faster version trillions of times to get the invested time back. Is this piece of code really the bottleneck of the complete program? If not: Avoid "premature optimization" (search this term in the net in case of doubts).
  1 件のコメント
bethel o
bethel o 2016 年 11 月 15 日
Thanks Jan, I have ran tic toc test and the average time after 1M iteration is:
label 1: 7.1861s
label 2: 7.3500s
So I will use the label 1. Regarding premature optimization, thanks for pointing that out; its just that I have a near-realtime code which require a lot of these assignments and the samplerate my be increased so I guess these will at some point have an impart when the samplerate is very high

サインインしてコメントする。

その他の回答 (1 件)

James Tursa
James Tursa 2016 年 11 月 15 日
編集済み: James Tursa 2016 年 11 月 15 日
Another method to try that might be faster for your application:
%%(3) or is this faster
c_mem(1:end-1)=c_mem(2:end);
c_mem(end)=c;
  2 件のコメント
bethel o
bethel o 2016 年 11 月 16 日
編集済み: bethel o 2016 年 11 月 16 日
Thanks James for this. Here is the result including label 3 from James using 1M iterations:
label 1: 7.1384s
label 2: 7.3883s
label 3: 6.5292s
This very interesting for me because I was feeling that label 1 will be faster than label 3 because it comprises one line of code. I was thinking that interpreted language works by compiling the code line by line and therefore will be faster for lesser line of code; but I was obviously wrong. I am not sure why label 3 is the fastest?
Jan
Jan 2016 年 11 月 16 日
It seems like [c_mem(2:end);c] creates the vector c_mem(2:end) explicitely and a new vector, when the last element is added. James solution moves the values inside the existing array whithout allocating a new one.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTarget Computer Setup についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by