Which of these two assignments is more efficient

1 vue (au cours des 30 derniers jours)
bethel o
bethel o le 15 Nov 2016
Commenté : Jan le 16 Nov 2016
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 commentaire
KSSV
KSSV le 15 Nov 2016
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.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 15 Nov 2016
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 commentaire
bethel o
bethel o le 15 Nov 2016
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

Connectez-vous pour commenter.

Plus de réponses (1)

James Tursa
James Tursa le 15 Nov 2016
Modifié(e) : James Tursa le 15 Nov 2016
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 commentaires
bethel o
bethel o le 16 Nov 2016
Modifié(e) : bethel o le 16 Nov 2016
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 le 16 Nov 2016
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.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by