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