It seems that it might be possible to make this loop faster. Does anyone have any thoughts? I have to call a loop like this millions of times in a larger workflow, and it is getting to be the slowest part of the code. I appreciate any insights!
I ran that loop a million times with n=100 and it took only 0.33 seconds. How long does it take for you? How fast do you need it to be.
Also, what physical process is this supposed to simulate? I'm wondering because some of the values get up to 10^55 which is extremely large for any real world situation.
Good questions... this is part of a recursive fast multipole method that I'm working on speeding up. This loop is nested inside 3 others, so it can get called a bunch of times and if n is large it can take some time. This loop is pretty fast already, like you mentioned, but since it could get called so many times a small improvment could pay off. The a and b values I picked at first were arbitrary, but really they are going to be between -1 and 1. Also typical initial values of c and s are 1 and 0.5 respectively. One last thing I was thinking: if this loop could be turned into some kind of matrix operation then I can eliminate it altogether to get a speed up. I wasn't sure that was possible, so I thought I would put it out there to see if someone else had an idea. I appreciate the comments!
Thanks everyone for the ideas! It would seem that the original for loop is the fastest by a good bit (see below). Any other ideas? I'm good with the speed if it is actually the fastest. Note that I changed the initial conditions and the a & b values from the original posting to be more realistic.
Nloops = 1e6;
c_init = 1;
s_init = 0.5;
a = 0.2; % constant
b = 0.3; % constant
n = 10; % number of elements of outputs c and s
% n could be up to 100
% Preallocate to speed up
c = zeros(1, n);
s = zeros(1, n);
c(1) = c_init; % set the initial condition for c
s(1) = s_init; % set the initial condition for s
% run the loop
tic
for k = 1 : Nloops
for i = 2:n
c(i) = a*c(i-1) -b*s(i-1);
s(i) = b*c(i-1) + a*s(i-1);
end
end
t1 = toc
t1 = 0.0661
% Method 2
% Preallocate to speed up
ct = zeros(1, n);
st = zeros(1, n);
ct(1) = c_init; % set the initial condition for c
st(1) = s_init; % set the initial condition for s
M = [a, -b; b, a];
tic
for k = 1 : Nloops
for i = 2:n
m = M * [ct(i-1); st(i-1)];
ct(i) = m(1);
st(i) = m(2);
end
end
t2 = toc
t2 = 1.0441
ratio = t2/t1
ratio = 15.7967
isequal(c,ct)
ans = logical
1
isequal(s,st)
ans = logical
1
% Method 3
% Preallocate to speed up
ct3 = zeros(1, n);
st3 = zeros(1, n);
ct3(1) = c_init; % set the initial condition for c
st3(1) = s_init; % set the initial condition for s
tic
for k = 1 : Nloops
for i = 2:n
m = M^(i-1) * [ct3(1); st3(1)];
ct3(i) = m(1);
st3(i) = m(2);
end
end
t3 = toc
t3 = 3.9479
% isequal(c,ct3) % this turns up false, but they are essentially identical
% isequal(s,st3) % this turns up false, but they are essentially identical
Impossibile completare l'azione a causa delle modifiche apportate alla pagina. Ricarica la pagina per vedere lo stato aggiornato.
Translated by
Seleziona un sito web
Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .
Puoi anche selezionare un sito web dal seguente elenco:
Come ottenere le migliori prestazioni del sito
Per ottenere le migliori prestazioni del sito, seleziona il sito cinese (in cinese o in inglese). I siti MathWorks per gli altri paesi non sono ottimizzati per essere visitati dalla tua area geografica.