Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
bsxfun runs faster in batches

Subject: bsxfun runs faster in batches

From: Daniel

Date: 4 Jul, 2012 12:56:06

Message: 1 of 7

I want to add a column vector, length 66, to all columns in a matrix with about a million columns.

You would think that a single call to bsxfun would be the fastest way to do this - I certainly thought so - but batch processing a few thousand columns at a time seems to go somewhere between 2 or 3 times as fast...results are inconsistent (due to the details of memory management I suppose). I'm running in Matlab 2012a on a Mac.

a = rand(66,1E6);
b = rand(66,1);

tic
a = bsxfun(@plus,a,b);
toc;

tic
for ii=1:1E4:1E6
    a(:,ii:ii+1E4-1) = bsxfun(@plus,a(:,ii:ii+1E4-1),b);
end
toc;

Is this behavior reproducible, and if so why does bsxfun self-optimise?
Also, is there a nice way to choose the batch size, or do I just have to try it with different values on every computer I might want to run the code on.

Thanks

Subject: bsxfun runs faster in batches

From: Daniel

Date: 4 Jul, 2012 13:03:09

Message: 2 of 7

At the bottom of my message, I meant why *doesn't* bsxfun self optimise.

Subject: bsxfun runs faster in batches

From: Nasser M. Abbasi

Date: 4 Jul, 2012 15:53:44

Message: 3 of 7

On 7/4/2012 7:56 AM, Daniel wrote:

>
> You would think that a single call to bsxfun would be the fastest way to do this -
>I certainly thought so - but batch processing a few thousand columns at a time seems
>to go somewhere between 2 or 3 times as fast...results are inconsistent
>(due to the details of memory management I suppose). I'm running in Matlab 2012a on a Mac.
>
> a = rand(66,1E6);
> b = rand(66,1);
>
> tic
> a = bsxfun(@plus,a,b);
> toc;
>
> tic
> for ii=1:1E4:1E6
> a(:,ii:ii+1E4-1) = bsxfun(@plus,a(:,ii:ii+1E4-1),b);
> end
> toc;
>

Not on windows 7. I have 2012a:

EDU>> clear all
EDU>> foo
one call speed = 0.102198 second
batch call speed = 0.443221 second

EDU>> foo
one call speed = 0.102137 second
batch call speed = 0.460130 second

EDU>> foo
one call speed = 0.101518 second
batch call speed = 0.440361 second

EDU>> foo
one call speed = 0.100628 second
batch call speed = 0.441858 second

-----------------
function foo()
a = rand(66,1E6);
b = rand(66,1);

tic
a = bsxfun(@plus,a,b);
t=toc;
fprintf('one call speed = %0.6f second\n',t);

tic
for ii=1:1E4:1E6
     a(:,ii:ii+1E4-1) = bsxfun(@plus,a(:,ii:ii+1E4-1),b);
end
t=toc;
fprintf('batch call speed = %0.6f second\n',t);
------------------

--Nasser

Subject: bsxfun runs faster in batches

From: Guven

Date: 4 Jul, 2012 21:11:07

Message: 4 of 7

Well, I have slightly edited the code to see all effects :
a = rand(66,1E6);
b = rand(66,1);

tic
a = bsxfun(@plus,a,b);
toc;

tic
for ii=1:1E4:1E6
    a(:,ii:ii+1E4-1) = bsxfun(@plus,a(:,ii:ii+1E4-1),b);
end
toc;

tic
for ii=1:1E6
    a(:,ii) = a(:,ii)+b;
end
toc

And the results are interesting, it got faster (might be due to other funcs or memory optimization) each time until they all hit the speed barrier. But the slowest was bsxfun in each execution. I am using Matlab 2012a on a mac. In the first execution I used your original code with last part omitted that's why there are only two time measurements.

>> den
Elapsed time is 20.006291 seconds.
Elapsed time is 1.402610 seconds.
>> den
Elapsed time is 4.113078 seconds.
Elapsed time is 0.612159 seconds.
Elapsed time is 0.597096 seconds.
>> den
Elapsed time is 0.590009 seconds.
Elapsed time is 0.525985 seconds.
Elapsed time is 0.521055 seconds.
>> den
Elapsed time is 0.592897 seconds.
Elapsed time is 0.526452 seconds.
Elapsed time is 0.522358 seconds.

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jt1ou8$4jc$1@speranza.aioe.org>...
> On 7/4/2012 7:56 AM, Daniel wrote:
>
> >
> > You would think that a single call to bsxfun would be the fastest way to do this -
> >I certainly thought so - but batch processing a few thousand columns at a time seems
> >to go somewhere between 2 or 3 times as fast...results are inconsistent
> >(due to the details of memory management I suppose). I'm running in Matlab 2012a on a Mac.
> >
> > a = rand(66,1E6);
> > b = rand(66,1);
> >
> > tic
> > a = bsxfun(@plus,a,b);
> > toc;
> >
> > tic
> > for ii=1:1E4:1E6
> > a(:,ii:ii+1E4-1) = bsxfun(@plus,a(:,ii:ii+1E4-1),b);
> > end
> > toc;
> >
>
> Not on windows 7. I have 2012a:
>
> EDU>> clear all
> EDU>> foo
> one call speed = 0.102198 second
> batch call speed = 0.443221 second
>
> EDU>> foo
> one call speed = 0.102137 second
> batch call speed = 0.460130 second
>
> EDU>> foo
> one call speed = 0.101518 second
> batch call speed = 0.440361 second
>
> EDU>> foo
> one call speed = 0.100628 second
> batch call speed = 0.441858 second
>
> -----------------
> function foo()
> a = rand(66,1E6);
> b = rand(66,1);
>
> tic
> a = bsxfun(@plus,a,b);
> t=toc;
> fprintf('one call speed = %0.6f second\n',t);
>
> tic
> for ii=1:1E4:1E6
> a(:,ii:ii+1E4-1) = bsxfun(@plus,a(:,ii:ii+1E4-1),b);
> end
> t=toc;
> fprintf('batch call speed = %0.6f second\n',t);
> ------------------
>
> --Nasser
>
>

Subject: bsxfun runs faster in batches

From: per isakson

Date: 5 Jul, 2012 00:17:08

Message: 5 of 7

It depends.

R2012a, 64bit, Windows7,
Dell, OptiPlex 960: 1 cpu, 4 cores, 8GB ram

Run as script:
Elapsed time is 4.856221 seconds.
Elapsed time is 9.488838 seconds.
Elapsed time is 3.403116 seconds.

Run as function:
Elapsed time is 2.525467 seconds.
Elapsed time is 9.335100 seconds.
Elapsed time is 3.374967 seconds.

function cssm
%%
a = rand(66,1E6);
b = rand(66,1);
%%
tic
for j = 1:10
a = bsxfun(@plus,a,b);
end
toc
tic
for j = 1:10
for ii=1:1E4:1E6
    a(:,ii:ii+1E4-1) = bsxfun(@plus,a(:,ii:ii+1E4-1),b);
end
end
toc
tic
for j = 1:10
for ii=1:1E6
    a(:,ii) = a(:,ii)+b;
end
end
toc
end

Subject: bsxfun runs faster in batches

From: Bruno Luong

Date: 5 Jul, 2012 05:08:16

Message: 6 of 7

"Daniel" wrote in message <jt1eh6$ino$1@newscl01ah.mathworks.com>...
>
> Is this behavior reproducible, and if so why does bsxfun self-optimise?

A single call of bsxfun is abut 4 times faster to me (2012a 64 bits, Windows7).

Bruno

Subject: bsxfun runs faster in batches

From: Juliette Salexa

Date: 25 Dec, 2012 01:03:12

Message: 7 of 7

Daniel,
Just remember that MATLAB, like many programs, are (thankfully) optimized for Windows.
I'd rather they spend more time improving speed, functionality, etc. for one operating system than to dilute their efforts on optimizing other operating for systems.

Merry Christmas!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us