필터 지우기
필터 지우기

Which of these two assignments is more efficient

조회 수: 1 (최근 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 CenterFile Exchange에서 Target Computer Setup에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by