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:
Speed difference between row and column vectors

Subject: Speed difference between row and column vectors

From: Joel

Date: 18 Apr, 2013 15:02:07

Message: 1 of 8

Can anyone tell what's going on here? I have a hard time believing MATLAB is so much slower when building column vectors.

>> tic; for i=1:1e6; x=5.*[1, 2, 3]; end; toc
Elapsed time is 0.389357 seconds.
>> tic; for i=1:1e6; x=5.*([1, 2, 3]'); end; toc
Elapsed time is 0.620597 seconds.
>> tic; for i=1:1e6; x=5.*[1; 2; 3]; end; toc
Elapsed time is 8.928639 seconds.

They delay seems to not be in the multiplication operation, because in the second example, the multiplication happens after the transpose.

Thoughts?

Subject: Speed difference between row and column vectors

From: Steven_Lord

Date: 18 Apr, 2013 15:31:26

Message: 2 of 8



"Joel " <joel.j.k.parker@nasa.gov> wrote in message
news:kkp1tf$au2$1@newscl01ah.mathworks.com...
> Can anyone tell what's going on here? I have a hard time believing MATLAB
> is so much slower when building column vectors.
>
>>> tic; for i=1:1e6; x=5.*[1, 2, 3]; end; toc
> Elapsed time is 0.389357 seconds.
>>> tic; for i=1:1e6; x=5.*([1, 2, 3]'); end; toc
> Elapsed time is 0.620597 seconds.
>>> tic; for i=1:1e6; x=5.*[1; 2; 3]; end; toc
> Elapsed time is 8.928639 seconds.
>
> They delay seems to not be in the multiplication operation, because in the
> second example, the multiplication happens after the transpose.
>
> Thoughts?

1) What version of MATLAB are you using?
2) What operating system are you using?
3) Are you doing this in a function, in a script, or at the command prompt?
4) What else was going on when that third loop ran? Was some other
application chewing up your system memory, requiring the OS to spend time
swapping that application? Was something "phoning home" and again, using up
your system's processing power for a little bit? My suspicion is that
something started during the last 10% or so of the second loop's execution
(causing a small slowdown) and continued through the entirety of the third
loop's execution (causing a large slowdown.)

I was not able to reproduce these results using a function in release R2013a
on my 64-bit Windows machine. All the elements of mt from the code below
were fairly close to one another.

T = zeros(10, 3);
for k = 1:10
    T(k, :) = colvec_test;
end
mt = mean(T)


where:

function T = colvec_test
T = zeros(1, 3);
tic;
for i=1:1e6;
    x=5.*[1, 2, 3];
end;
tx = toc;
T(1) = tx;

tic;
for i=1:1e6;
    x=5.*([1, 2, 3]');
end;
tx = toc;
T(2) = tx;

tic;
for i=1:1e6;
    x=5.*[1; 2; 3];
end;
tx = toc;
T(3) = tx;


--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Speed difference between row and column vectors

From: Joel

Date: 18 Apr, 2013 15:51:13

Message: 3 of 8

"Steven_Lord" <slord@mathworks.com> wrote in message <kkp3kd$gfd$1@newscl01ah.mathworks.com>...
>
>
> "Joel " <joel.j.k.parker@nasa.gov> wrote in message
> news:kkp1tf$au2$1@newscl01ah.mathworks.com...
> > Can anyone tell what's going on here? I have a hard time believing MATLAB
> > is so much slower when building column vectors.
> >
> >>> tic; for i=1:1e6; x=5.*[1, 2, 3]; end; toc
> > Elapsed time is 0.389357 seconds.
> >>> tic; for i=1:1e6; x=5.*([1, 2, 3]'); end; toc
> > Elapsed time is 0.620597 seconds.
> >>> tic; for i=1:1e6; x=5.*[1; 2; 3]; end; toc
> > Elapsed time is 8.928639 seconds.
> >
> > They delay seems to not be in the multiplication operation, because in the
> > second example, the multiplication happens after the transpose.
> >
> > Thoughts?
>
> 1) What version of MATLAB are you using?
> 2) What operating system are you using?
> 3) Are you doing this in a function, in a script, or at the command prompt?
> 4) What else was going on when that third loop ran? Was some other
> application chewing up your system memory, requiring the OS to spend time
> swapping that application? Was something "phoning home" and again, using up
> your system's processing power for a little bit? My suspicion is that
> something started during the last 10% or so of the second loop's execution
> (causing a small slowdown) and continued through the entirety of the third
> loop's execution (causing a large slowdown.)
>
> I was not able to reproduce these results using a function in release R2013a
> on my 64-bit Windows machine. All the elements of mt from the code below
> were fairly close to one another.
>
> T = zeros(10, 3);
> for k = 1:10
> T(k, :) = colvec_test;
> end
> mt = mean(T)
>
>
> where:
>
> function T = colvec_test
> T = zeros(1, 3);
> tic;
> for i=1:1e6;
> x=5.*[1, 2, 3];
> end;
> tx = toc;
> T(1) = tx;
>
> tic;
> for i=1:1e6;
> x=5.*([1, 2, 3]');
> end;
> tx = toc;
> T(2) = tx;
>
> tic;
> for i=1:1e6;
> x=5.*[1; 2; 3];
> end;
> tx = toc;
> T(3) = tx;
>
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Thanks for the reply.

I'm using MATLAB R2013a 64-bit on Windows 7 Enterprise 64-bit.

When I run your function colvec_test (displaying output instead of saving it to T), I get:

>> colvec_test
Elapsed time is 0.078862 seconds.
Elapsed time is 0.079289 seconds.
Elapsed time is 0.078867 seconds.
>>

When I run the contents of colvec_test from the command prompt, I get:

>> [snip]
Elapsed time is 0.392249 seconds.
Elapsed time is 0.643703 seconds.
Elapsed time is 9.023197 seconds.
>>

Subject: Speed difference between row and column vectors

From: Steven_Lord

Date: 18 Apr, 2013 16:19:25

Message: 4 of 8



"Joel " <joel.j.k.parker@nasa.gov> wrote in message
news:kkp4ph$kj0$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <kkp3kd$gfd$1@newscl01ah.mathworks.com>...

> Thanks for the reply.
>
> I'm using MATLAB R2013a 64-bit on Windows 7 Enterprise 64-bit.
>
> When I run your function colvec_test (displaying output instead of saving
> it to T), I get:
>
>>> colvec_test
> Elapsed time is 0.078862 seconds.
> Elapsed time is 0.079289 seconds.
> Elapsed time is 0.078867 seconds.
>>>
>
> When I run the contents of colvec_test from the command prompt, I get:
>
>>> [snip]
> Elapsed time is 0.392249 seconds.
> Elapsed time is 0.643703 seconds.
> Elapsed time is 9.023197 seconds.
>>>

Hmm ... there shouldn't be that kind of difference in the performance. I'll
report this to the development staff.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Speed difference between row and column vectors

From: Bruno Luong

Date: 18 Apr, 2013 16:32:11

Message: 5 of 8

"Joel " <joel.j.k.parker@nasa.gov> wrote in message <kkp1tf$au2$1@newscl01ah.mathworks.com>...
> Can anyone tell what's going on here? I have a hard time believing MATLAB is so much slower when building column vectors.
>
> >> tic; for i=1:1e6; x=5.*[1, 2, 3]; end; toc
> Elapsed time is 0.389357 seconds.
> >> tic; for i=1:1e6; x=5.*([1, 2, 3]'); end; toc
> Elapsed time is 0.620597 seconds.
> >> tic; for i=1:1e6; x=5.*[1; 2; 3]; end; toc
> Elapsed time is 8.928639 seconds.
>

Try this:

function benchloop
tic;
for i=1:1e6;
    x=5.*[1, 2, 3];
end
toc
tic
for i=1:1e6
    x=5.*([1, 2, 3]');
end
toc
tic
for i=1:1e6
    x=5.*[1; 2; 3];
end
toc

I repeat what I always repeat: never bench in script, never scramble many statements in the same line of code.

Bruno

Subject: Speed difference between row and column vectors

From: Bruno Luong

Date: 18 Apr, 2013 16:35:09

Message: 6 of 8

"Steven_Lord" <slord@mathworks.com> wrote in message
> Hmm ... there shouldn't be that kind of difference in the performance. I'll
> report this to the development staff.

Steve, can you ask please them why it is so difficult to implement jit accelerator with scripting code?

Bruno

Subject: Speed difference between row and column vectors

From: Loren Shure

Date: 19 Apr, 2013 10:33:15

Message: 7 of 8


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
news:kkp7bt$shi$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
>> Hmm ... there shouldn't be that kind of difference in the performance.
>> I'll report this to the development staff.
>
> Steve, can you ask please them why it is so difficult to implement jit
> accelerator with scripting code?
>
> Bruno
>

It's at least in part because there is no guarantee what the script will
find in the workspace and memory before the script has been run. In a
function, the environment is much more controlled and understood.

--
--Loren

http://blogs.mathworks.com/loren

Subject: Speed difference between row and column vectors

From: Bruno Luong

Date: 19 Apr, 2013 11:55:09

Message: 8 of 8

"Loren Shure" wrote in message <kkr6hb$n44$1@newscl01ah.mathworks.com>...
>
> It's at least in part because there is no guarantee what the script will
> find in the workspace and memory before the script has been run. In a
> function, the environment is much more controlled and understood.

What escapes me is what is the difference between the base workspace and function workspace?

On the user point of view, what are things that are more flexible of the base workspace?

If internally the base workspace was encapsulated with a superbe hidden function workspace, what users would lost?

Bruno

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