From: Andreas Lobinger on
Andreas Lobinger <tvask(a)biszumknie.de> wrote in message
> On Thu, 10 Jun 2010 14:37:04 +0000, Matt J wrote:
> > Andreas Lobinger <tvask(a)biszumknie.de> wrote in message
> > profiler tells me, that using bsxfun is slower than the
> >> >> initial solution (?).
> >
> > According to my tests below, bsxfun is faster. The difference also gets
> > bigger as a and b get bigger (as expected).

I post here the output of the profiler, sorry for the formatting, but i had to
copy from .pdf printout (don't ask...)

OLDdistance_wrapped (1000 calls, 7.980 sec)
time calls line
0.52 1000 1 mpos_mat = repmat(UEcoo.',length(net.wrap_positions),1);
2
0.16 1000 3 ddistances = zeros(length(geom.site_pos),n_users);
0.02 1000 4 for l=1:length(geom.site_pos)
1.79 12000 5 m2 = mpos_mat - geom.bpos_k{l};
1.35 12000 6 m1 = real(m2).^2 + imag(m2).^2;
7
2.23 12000 8 [temp2,ind] = min(m1);
9
10 % building the index out of subscripts is a lot faster
11 % than with sub2ind (why?)
12
1.20 12000 13 m3 = m2(( ...
14 0: ...
15 length(net.wrap_positions): ...
16 (n_users * length(net.wrap_positions))-1) + ind);
17
0.68 12000 18 ddistances(l,:) = m3;
19
0.02 12000 20 end

distance_wrapped (1000 calls, 9.423 sec)
time calls line
0.05 1000 2 ddistances = zeros(length(geom.site_pos),n_users);
3
1000 4 for l=1:length(geom.site_pos)
5
3.94 12000 6 m2 = bsxfun(@minus, UEcoo.', geom.site_pos(l) + net.wrap_positions);
1.43 12000 7 m1 = real(m2).^2 + imag(m2).^2;
8
2.18 12000 9 [temp2,ind] = min(m1);
10
11 % building the index out of subscripts is a lot faster
12 % than with sub2ind (why?)
13
1.29 12000 14 m3 = m2(( ...
15 0: ...
16 length(net.wrap_positions): ...
17 (n_users * length(net.wrap_positions))-1) + ind);
18
0.52 12000 19 ddistances(l,:) = m3;
20
0.02 12000 21 end

And the question remains, what i'm doing wrong?

Wishing a happy day,
LOBI
From: Jan Simon on
Dear Matt J!

> a=rand(648,1);
> b=rand(1,12);
> tic;
> d = repmat(a,1,size(b,2)) - repmat(b,size(a,1),1);
> toc
> %Elapsed time is 0.000362 seconds.
>
> clear d
> tic;
> d=bsxfun(@minus,a,b);
> toc
> %Elapsed time is 0.000106 seconds.

You can avoid the overhead of calling REPMAT:
a = rand(648,1);
b = rand(1,12);
tic;
d = a(:, ones(1, size(b,2))) - b(ones(1, size(a,1)), :);
toc

After some tests in Matlab 2009a it seems like the JIT recognizes the construction and does not create the temporary arrays. I tried this with large arrays, which would exceed the available free RAM.
To test the effect of the JIT, move it into a function. But the best test is the real program.

Good luck, Jan
First  |  Prev  | 
Pages: 1 2 3
Prev: Finding max of simple function
Next: Filter Design