[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: My vectorized code is slower than a loop
From: |
Francesco Potortì |
Subject: |
Re: My vectorized code is slower than a loop |
Date: |
Wed, 19 Oct 2016 22:19:03 +0200 |
No profiling, no runs, just looking at the code, so I may be off.
w = zeros(1, n);
j = 1
for i = 1:n
if (vectorised)
if(left(i) <= right(i))
w(j:j+right(i)-left(i)) = x(i) - x(n+1-[left(i):right(i)]);
j = j + right(i)-left(i)+1;
endif
else # looping
if(left(i) <= right(i))
for jj = left(i):right(i)
w(j) = x(i) - x(n+1-jj);
j += 1;
endfor
endif
endfor
In the loop version, the if is useless, because the for range will be
empty, so the for will not run when the if clause is false. This may
result in a speedup or not.
In the vectorised version, I would write it like this, which minimises
the number of operations at the expense of the number of instructions,
which may result in a speedup or not:
li = left(i);
d = right(i) - li;
if (d >= 0)
dr = 0:d;
w(j+dr) = x(i) - x(n+1-li+dr);
j += d+1;
endif
--
Francesco Potortì (ricercatore) Voice: +39.050.621.3058
ISTI - Area della ricerca CNR Mobile: +39.348.8283.107
via G. Moruzzi 1, I-56124 Pisa Skype: wnlabisti
(entrance 20, 1st floor, room C71) Web: http://fly.isti.cnr.it