The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: bsxfun runs faster in batches
Replies: 6   Last Post: Dec 24, 2012 8:03 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 2
Registered: 7/4/12
bsxfun runs faster in batches
Posted: Jul 4, 2012 8:56 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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);

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

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

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.


Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.