From: Andreas Lobinger on
On Thu, 10 Jun 2010 08:39:04 -0400, Peter Perkins wrote:

> On 6/10/2010 2:33 AM, Andreas Lobinger wrote:
>> in the SW i'm working on, there are some operations which f.e. build a
>> 'cross' difference, so from all elements from one vector to all
>> elements in the second vector.
>>
>> d = repmat(a,1,size(b,2)) - repmat(b,size(a,1),1);
>
>> However, the profiler tells me, that using bsxfun is slower than the
>> initial solution (?).
>
> LOBI, how big are a and b?

size(a,1) ~ 200 ... 2000
size(b,2) ~ 10 ... 30

My profile testcase was a 648 and b 12.

Hope that helps.

Wishing a happy day,
LOBI
From: Oliver Woodford on
"Matt Fig" wrote:
> You might be thinking of the limited string inputs to CELLFUN, which are indeed much faster than using equivalent function handles. I wish TMW would increase the number of these for CELLFUN and add some for BSXFUN.

Agreed. And also for accumarray.
From: Matt J on
Andreas Lobinger <tvask(a)biszumknie.de> wrote in message <987fd$4c10f0e9$4fe5d74a$23179(a)news1.surfino.com>...
profiler tells me, that using bsxfun is slower than the
> >> initial solution (?).
> >
> > LOBI, how big are a and b?
>
> size(a,1) ~ 200 ... 2000
> size(b,2) ~ 10 ... 30
>
> My profile testcase was a 648 and b 12.

According to my tests below, bsxfun is faster. The difference also gets bigger as a and b get bigger (as expected).


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.
From: Andreas Lobinger on
Aloha,

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 agree with you and in other parts of the program bsxfun is faster.
But somehow there seems to be side effects. I'll post the output of
the profile later, when i'm back at the 'other' computer.

Wishing a happy day,
LOBI
From: John D'Errico on
Andreas Lobinger <tvask(a)biszumknie.de> wrote in message <c5b3e$4c10fc19$4fe5d74a$23179(a)news1.surfino.com>...
> Aloha,
>
> 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 agree with you and in other parts of the program bsxfun is faster.
> But somehow there seems to be side effects. I'll post the output of
> the profile later, when i'm back at the 'other' computer.
>
> Wishing a happy day,
> LOBI

bsxfun IS faster in my tests too.

A = rand(100000,3);
B = rand(1,3);

timeit(@() sum(A - repmat(B,size(A,1),1).^2,2))
ans =
0.003722836266

timeit(@() sum(bsxfun(@minus,A,B).^2,2))
ans =
0.001830291266

John
First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: Finding max of simple function
Next: Filter Design