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:
Time consuming power operation

Subject: Time consuming power operation

From: Morten Gjerding

Date: 20 Aug, 2010 14:19:04

Message: 1 of 6

Hi there matlab users.

I have a program that im running some optimization routines on using the profiler. I appears that the following line takes more time than I initially expected:

change = sqrt(sum((vectorChange).^2,2));

The variable "vectorChange" is just a three component (x,y,z) vector. The line is processed many million times during execution. My question is: What is taking so much time in this line, and why? And what can i do? I find it a little puzzling that this line takes so much time.

Regards, Morten.

Subject: Time consuming power operation

From: Sean

Date: 20 Aug, 2010 14:31:22

Message: 2 of 6

"Morten Gjerding" <s082177@student.dtu.dk> wrote in message <i4m2so$4fb$1@fred.mathworks.com>...
> Hi there matlab users.
>
> I have a program that im running some optimization routines on using the profiler. I appears that the following line takes more time than I initially expected:
>
> change = sqrt(sum((vectorChange).^2,2));
>
> The variable "vectorChange" is just a three component (x,y,z) vector. The line is processed many million times during execution. My question is: What is taking so much time in this line, and why? And what can i do? I find it a little puzzling that this line takes so much time.
>
> Regards, Morten.

Well it is being called millions of times as you said. That would take some execution time.

What else are you doing, can you vectorize other things to minimize how many times this is called?

Subject: Time consuming power operation

From: Walter Roberson

Date: 20 Aug, 2010 14:39:02

Message: 3 of 6

On 20/08/10 9:19 AM, Morten Gjerding wrote:

> I have a program that im running some optimization routines on using the
> profiler. I appears that the following line takes more time than I
> initially expected:
>
> change = sqrt(sum((vectorChange).^2,2));
> The variable "vectorChange" is just a three component (x,y,z) vector.
> The line is processed many million times during execution. My question
> is: What is taking so much time in this line, and why? And what can i
> do? I find it a little puzzling that this line takes so much time.

You might get better performance if you were to switch the vectors from
being row vectors to being column vectors.

Also, you might get better performance if you were to store the sum in a
temporary variable and multiply that variable by itself rather than
using .^2 .

Subject: Time consuming power operation

From: Morten Gjerding

Date: 21 Aug, 2010 12:45:05

Message: 4 of 6

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i4m3jp$l2p$1@fred.mathworks.com>...
> "Morten Gjerding" <s082177@student.dtu.dk> wrote in message <i4m2so$4fb$1@fred.mathworks.com>...
> > Hi there matlab users.
> >
> > I have a program that im running some optimization routines on using the profiler. I appears that the following line takes more time than I initially expected:
> >
> > change = sqrt(sum((vectorChange).^2,2));
> >
> > The variable "vectorChange" is just a three component (x,y,z) vector. The line is processed many million times during execution. My question is: What is taking so much time in this line, and why? And what can i do? I find it a little puzzling that this line takes so much time.
> >
> > Regards, Morten.
>
> Well it is being called millions of times as you said. That would take some execution time.
>
> What else are you doing, can you vectorize other things to minimize how many times this is called?


True :), of course it takes some time when it is processed so many times, but relative to other lines this takes much more time to process than expected.

Unfortunately, I have already done all the vectorization optimizations that i could think of.

Subject: Time consuming power operation

From: Morten Gjerding

Date: 21 Aug, 2010 12:48:03

Message: 5 of 6

Walter Roberson <roberson@hushmail.com> wrote in message <bswbo.58634$f_3.21887@newsfe17.iad>...
> On 20/08/10 9:19 AM, Morten Gjerding wrote:
>
> > I have a program that im running some optimization routines on using the
> > profiler. I appears that the following line takes more time than I
> > initially expected:
> >
> > change = sqrt(sum((vectorChange).^2,2));
> > The variable "vectorChange" is just a three component (x,y,z) vector.
> > The line is processed many million times during execution. My question
> > is: What is taking so much time in this line, and why? And what can i
> > do? I find it a little puzzling that this line takes so much time.
>
> You might get better performance if you were to switch the vectors from
> being row vectors to being column vectors.
>
> Also, you might get better performance if you were to store the sum in a
> temporary variable and multiply that variable by itself rather than
> using .^2 .

Ok i Will try to see of it will be less time consuming implementing the changes you mention. One question: Why would it make a difference to change to column vectors?

Subject: Time consuming power operation

From: Walter Roberson

Date: 21 Aug, 2010 16:10:18

Message: 6 of 6

On 21/08/10 7:48 AM, Morten Gjerding wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message

>> > change = sqrt(sum((vectorChange).^2,2));

>> You might get better performance if you were to switch the vectors
>> from being row vectors to being column vectors.

> Ok i Will try to see of it will be less time consuming implementing the
> changes you mention. One question: Why would it make a difference to
> change to column vectors?

MATLAB is optimized for memory access down columns rather than across
rows. The CPU code to access memory locations known to be adjacent can
be a bit more efficient. I don't promise that you will be able to
measure the difference (as I do not know the implementation details of
MATLAB) but there is the theoretical possibility that might add up when
you do the computation repeatedly.

Tags for this Thread

No tags are associated with 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