Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: very slow vectorized code

Subject: very slow vectorized code

From: Julian Reichl

Date: 24 Apr, 2008 06:50:04

Message: 1 of 5

Hi all,
I have a problem that I can't figure out. I have two
versions of some code, one vectorised, the other not. The
vectorised version runs WAY slower for single repeats - ~17
seconds cf. 0.3 seconds.

The profiler tells me that pretty much every line of the
code is a lot slower in the vectorised version. Even simple
things like

e=repmat(1,1);
for i=1:28305
e(:,:)=0;
end

(the 1:28305 is a time-stepping loop and can't be removed)
takes 0.39 s inside the vectorised code (not including the
repmat), whereas in the command line it takes less than a
10th of that time.
if, in the same code (the vectorised version), I replace it
with

for i=1:28305
e=0;
end

(which is fine for this example because it's a 1x1, but not
for my application), the time for that function drops,
again, to ~ a 10th.

I really hope someone has an idea!!!

thanks

Julian

Subject: Re: very slow vectorized code

From: Julian Reichl

Date: 24 Apr, 2008 07:07:02

Message: 2 of 5

I just tried commenting out all the other code within the loop
i=1:28305
...
end
and it bought that e(:,:)=0;
statement back to 0.04s. So could this mean that there is
some memory issue?

"Julian Reichl" <julesreichl@yahoo.co.uk> wrote in message
<fupais$8av$1@fred.mathworks.com>...
> Hi all,
> I have a problem that I can't figure out. I have two
> versions of some code, one vectorised, the other not. The
> vectorised version runs WAY slower for single repeats - ~17
> seconds cf. 0.3 seconds.
>
> The profiler tells me that pretty much every line of the
> code is a lot slower in the vectorised version. Even simple
> things like
>
> e=repmat(1,1);
> for i=1:28305
> e(:,:)=0;
> end
>
> (the 1:28305 is a time-stepping loop and can't be removed)
> takes 0.39 s inside the vectorised code (not including the
> repmat), whereas in the command line it takes less than a
> 10th of that time.
> if, in the same code (the vectorised version), I replace it
> with
>
> for i=1:28305
> e=0;
> end
>
> (which is fine for this example because it's a 1x1, but not
> for my application), the time for that function drops,
> again, to ~ a 10th.
>
> I really hope someone has an idea!!!
>
> thanks
>
> Julian
>

Subject: Re: very slow vectorized code

From: Bill Nell

Date: 25 Apr, 2008 15:26:17

Message: 3 of 5

Julian Reichl wrote:
> I just tried commenting out all the other code within the loop
> i=1:28305
> ...
> end
> and it bought that e(:,:)=0;
> statement back to 0.04s. So could this mean that there is
> some memory issue?
>
> "Julian Reichl" <julesreichl@yahoo.co.uk> wrote in message
> <fupais$8av$1@fred.mathworks.com>...
>> Hi all,
>> I have a problem that I can't figure out. I have two
>> versions of some code, one vectorised, the other not. The
>> vectorised version runs WAY slower for single repeats - ~17
>> seconds cf. 0.3 seconds.
>>
>> The profiler tells me that pretty much every line of the
>> code is a lot slower in the vectorised version. Even simple
>> things like
>>
>> e=repmat(1,1);
>> for i=1:28305
>> e(:,:)=0;
>> end
>>
>> (the 1:28305 is a time-stepping loop and can't be removed)
>> takes 0.39 s inside the vectorised code (not including the
>> repmat), whereas in the command line it takes less than a
>> 10th of that time.
>> if, in the same code (the vectorised version), I replace it
>> with
>>
>> for i=1:28305
>> e=0;
>> end
>>
>> (which is fine for this example because it's a 1x1, but not
>> for my application), the time for that function drops,
>> again, to ~ a 10th.
>>
>> I really hope someone has an idea!!!
>>
>> thanks
>>
>> Julian
>>
>

Julian,
If you are just trying to set the contents of e to 0 then just use
e(:) = 0 instead of e(:,:) = 0.

Bill

Subject: Re: very slow vectorized code

From: Yi Cao

Date: 25 Apr, 2008 19:18:01

Message: 4 of 5

"Julian Reichl" <julesreichl@yahoo.co.uk> wrote in message
<fupbim$hs6$1@fred.mathworks.com>...
> I just tried commenting out all the other code within the
loop
> i=1:28305
> ...
> end
> and it bought that e(:,:)=0;
> statement back to 0.04s. So could this mean that there is
> some memory issue?
>
> "Julian Reichl" <julesreichl@yahoo.co.uk> wrote in message
> <fupais$8av$1@fred.mathworks.com>...
> > Hi all,
> > I have a problem that I can't figure out. I have two
> > versions of some code, one vectorised, the other not.
The
> > vectorised version runs WAY slower for single repeats -
~17
> > seconds cf. 0.3 seconds.
> >
> > The profiler tells me that pretty much every line of the
> > code is a lot slower in the vectorised version. Even
simple
> > things like
> >
> > e=repmat(1,1);
> > for i=1:28305
> > e(:,:)=0;
> > end
> >
> > (the 1:28305 is a time-stepping loop and can't be
removed)
> > takes 0.39 s inside the vectorised code (not including
the
> > repmat), whereas in the command line it takes less than
a
> > 10th of that time.
> > if, in the same code (the vectorised version), I
replace it
> > with
> >
> > for i=1:28305
> > e=0;
> > end
> >
> > (which is fine for this example because it's a 1x1, but
not
> > for my application), the time for that function drops,
> > again, to ~ a 10th.
> >
> > I really hope someone has an idea!!!
> >
> > thanks
> >
> > Julian
> >
>

I guess this is because JIT accelerator, i.e. your
vectorized code is quite complicated, hence the JIT
accelerator is not able to accelerate it, while your simple
loop version is in favour for the JIT to accelerate. When
you remove all other lines in the loop, the vectorized
version becomes simple as well, hence the JIT can
accelerate it now. That is why you see speed improvement.
To demonstrate this, you can try run your code with JIT on
and off by using the command:

feature accel on

or

feature accel off

to see if this is the case.

hth
Yi Cao

Subject: Re: very slow vectorized code

From: Julian Reichl

Date: 29 Apr, 2008 03:24:01

Message: 5 of 5

Thanks Bill and Yi Cao for your replies...
I'll check out the JIT thing and let you know how I go,

cheers
Julian

"Yi Cao" <y.cao@cranfield.ac.uk> wrote in message
<futap9$f0d$1@fred.mathworks.com>...
> "Julian Reichl" <julesreichl@yahoo.co.uk> wrote in message
> <fupbim$hs6$1@fred.mathworks.com>...
> > I just tried commenting out all the other code within the
> loop
> > i=1:28305
> > ...
> > end
> > and it bought that e(:,:)=0;
> > statement back to 0.04s. So could this mean that there is
> > some memory issue?
> >
> > "Julian Reichl" <julesreichl@yahoo.co.uk> wrote in message
> > <fupais$8av$1@fred.mathworks.com>...
> > > Hi all,
> > > I have a problem that I can't figure out. I have two
> > > versions of some code, one vectorised, the other not.
> The
> > > vectorised version runs WAY slower for single repeats -
> ~17
> > > seconds cf. 0.3 seconds.
> > >
> > > The profiler tells me that pretty much every line of the
> > > code is a lot slower in the vectorised version. Even
> simple
> > > things like
> > >
> > > e=repmat(1,1);
> > > for i=1:28305
> > > e(:,:)=0;
> > > end
> > >
> > > (the 1:28305 is a time-stepping loop and can't be
> removed)
> > > takes 0.39 s inside the vectorised code (not including
> the
> > > repmat), whereas in the command line it takes less than
> a
> > > 10th of that time.
> > > if, in the same code (the vectorised version), I
> replace it
> > > with
> > >
> > > for i=1:28305
> > > e=0;
> > > end
> > >
> > > (which is fine for this example because it's a 1x1, but
> not
> > > for my application), the time for that function drops,
> > > again, to ~ a 10th.
> > >
> > > I really hope someone has an idea!!!
> > >
> > > thanks
> > >
> > > Julian
> > >
> >
>
> I guess this is because JIT accelerator, i.e. your
> vectorized code is quite complicated, hence the JIT
> accelerator is not able to accelerate it, while your simple
> loop version is in favour for the JIT to accelerate. When
> you remove all other lines in the loop, the vectorized
> version becomes simple as well, hence the JIT can
> accelerate it now. That is why you see speed improvement.
> To demonstrate this, you can try run your code with JIT on
> and off by using the command:
>
> feature accel on
>
> or
>
> feature accel off
>
> to see if this is the case.
>
> hth
> Yi Cao
>

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
vectorize Julian Reichl 24 Apr, 2008 05:52:37
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics