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:
speedup code

Subject: speedup code

From: andrea182

Date: 9 Dec, 2009 16:22:33

Message: 1 of 9

can someone tell me how can i speed up this code:

lungo=5000;
for(t=lungo:-1:1)
   tsc(1,t)=(t-1)*(-60)
end

i can't change lungo...

it takes about 2-3 minutes...

the code is right but it is so slow....

thanks

Subject: speedup code

From: Matt Fig

Date: 9 Dec, 2009 16:42:18

Message: 2 of 9

There is no way that should take 3 minutes, unless you are letting each step of the loop dump to the screen. Even then it shouldn't take that long. Put a semicolon on your array creation!


lungo = 5000;

tic
for t = lungo:-1:1
   tsc(1,t)=(t-1)*(-60); % Notice the semicolon
end
toc

tic
Y = (0:lungo-1)*(-60);
toc

isequal(tsc,Y)



Elapsed time is 0.000158 seconds.
Elapsed time is 0.000101 seconds.
ans =
     1

Subject: speedup code

From: andrea182

Date: 9 Dec, 2009 16:57:21

Message: 3 of 9

"Matt Fig" <spamanon@yahoo.com> wrote in message <hfok1a$mac$1@fred.mathworks.com>...
> There is no way that should take 3 minutes, unless you are letting each step of the loop dump to the screen. Even then it shouldn't take that long. Put a semicolon on your array creation!
>
>
> lungo = 5000;
>
> tic
> for t = lungo:-1:1
> tsc(1,t)=(t-1)*(-60); % Notice the semicolon
> end
> toc
>
> tic
> Y = (0:lungo-1)*(-60);
> toc
>
> isequal(tsc,Y)
>
>
>
> Elapsed time is 0.000158 seconds.
> Elapsed time is 0.000101 seconds.
> ans =
> 1

i put the semicolon but it takes me a lot of time...it always write all the passage on the command window...

Subject: speedup code

From: dpb

Date: 9 Dec, 2009 18:41:28

Message: 4 of 9

andrea182 wrote:
> "Matt Fig" <spamanon@yahoo.com> wrote in message <hfok1a$mac$1@fred.mathworks.com>...
>> There is no way that should take 3 minutes, unless you are letting each step of the loop dump to the screen. Even then it shouldn't take that long. Put a semicolon on your array creation!
>>
>>
>> lungo = 5000;
>>
>> tic
>> for t = lungo:-1:1
>> tsc(1,t)=(t-1)*(-60); % Notice the semicolon
>> end
>> toc
>>
>> tic
>> Y = (0:lungo-1)*(-60);
>> toc
>>
>> isequal(tsc,Y)
>>
>>
>>
>> Elapsed time is 0.000158 seconds.
>> Elapsed time is 0.000101 seconds.
>> ans =
>> 1
>
> i put the semicolon but it takes me a lot of time...it always write
> all the passage on the command window...

There's something you're not showing us then -- the trailing semicolon
_will_ stop display.

--

Subject: speedup code

From: andrea182

Date: 9 Dec, 2009 18:54:03

Message: 5 of 9

dpb <none@non.net> wrote in message <hfor19$9sk$1@news.eternal-september.org>...
> andrea182 wrote:
> > "Matt Fig" <spamanon@yahoo.com> wrote in message <hfok1a$mac$1@fred.mathworks.com>...
> >> There is no way that should take 3 minutes, unless you are letting each step of the loop dump to the screen. Even then it shouldn't take that long. Put a semicolon on your array creation!
> >>
> >>
> >> lungo = 5000;
> >>
> >> tic
> >> for t = lungo:-1:1
> >> tsc(1,t)=(t-1)*(-60); % Notice the semicolon
> >> end
> >> toc
> >>
> >> tic
> >> Y = (0:lungo-1)*(-60);
> >> toc
> >>
> >> isequal(tsc,Y)
> >>
> >>
> >>
> >> Elapsed time is 0.000158 seconds.
> >> Elapsed time is 0.000101 seconds.
> >> ans =
> >> 1
> >
> > i put the semicolon but it takes me a lot of time...it always write
> > all the passage on the command window...
>
> There's something you're not showing us then -- the trailing semicolon
> _will_ stop display.
>
> --
that's all the code
clear all
load test.mat;
sc=derivato;
lungo=length(sc);
for t = lungo:-1:1
   tsc(1,t)=(t-1)*(-60); % Notice the semicolon
end
tsc=flipud(tsc);
%tsc=composite(:,1); %% tempi
sigma_l=std(diff(sc(1:end)));

Subject: speedup code

From: Oleg Komarov

Date: 9 Dec, 2009 19:18:04

Message: 6 of 9

> that's all the code
> clear all
> load test.mat;
> sc=derivato;
> lungo=length(sc);
> for t = lungo:-1:1
> tsc(1,t)=(t-1)*(-60); % Notice the semicolon
> end
> tsc=flipud(tsc);
> %tsc=composite(:,1); %% tempi
> sigma_l=std(diff(sc(1:end)));

type this:
profile on
...your code
profile off
profile viewer

and tell us what's taking most of the time.

Subject: speedup code

From: Yuri Geshelin

Date: 9 Dec, 2009 19:21:14

Message: 7 of 9

"andrea182 " <nickfasullo77@yahoo.it> wrote in message <hforob$jea$1@fred.mathworks.com>...
> dpb <none@non.net> wrote in message <hfor19$9sk$1@news.eternal-september.org>...
> > andrea182 wrote:
> > > "Matt Fig" <spamanon@yahoo.com> wrote in message <hfok1a$mac$1@fred.mathworks.com>...
> > >> There is no way that should take 3 minutes, unless you are letting each step of the loop dump to the screen. Even then it shouldn't take that long. Put a semicolon on your array creation!
> > >>
> > >>
> > >> lungo = 5000;
> > >>
> > >> tic
> > >> for t = lungo:-1:1
> > >> tsc(1,t)=(t-1)*(-60); % Notice the semicolon
> > >> end
> > >> toc
> > >>
> > >> tic
> > >> Y = (0:lungo-1)*(-60);
> > >> toc
> > >>
> > >> isequal(tsc,Y)
> > >>
> > >>
> > >>
> > >> Elapsed time is 0.000158 seconds.
> > >> Elapsed time is 0.000101 seconds.
> > >> ans =
> > >> 1
> > >
> > > i put the semicolon but it takes me a lot of time...it always write
> > > all the passage on the command window...
> >
> > There's something you're not showing us then -- the trailing semicolon
> > _will_ stop display.
> >
> > --
> that's all the code
> clear all
> load test.mat;
> sc=derivato;
> lungo=length(sc);
> for t = lungo:-1:1
> tsc(1,t)=(t-1)*(-60); % Notice the semicolon
> end
> tsc=flipud(tsc);
> %tsc=composite(:,1); %% tempi
> sigma_l=std(diff(sc(1:end)));

You could replace the FOR loop with just

tsc(1,:) = 0:-60:-299940;

(and by the way, this is a bit more accurate).

But this does not really speed up your code. As Matt Fig said, there should not be any output with each pass of the loop.

Yuri

Subject: speedup code

From: Matt Fig

Date: 9 Dec, 2009 19:27:03

Message: 8 of 9

I already showed you how to vectorize your FOR loop for some speed gain. I would be willing to bet that loading the file is the largest time-hog in your program. Either that or the unspecified derivato function (assuming that isn't a variable popped into existence by the load).

Also, what is the purpose of using FLIPUD on a row vector?

Subject: speedup code

From: andrea182

Date: 9 Dec, 2009 19:45:33

Message: 9 of 9

"Yuri Geshelin" <geshelin@hotmail.com> wrote in message <hfotba$4f8$1@fred.mathworks.com>...
> "andrea182 " <nickfasullo77@yahoo.it> wrote in message <hforob$jea$1@fred.mathworks.com>...
> > dpb <none@non.net> wrote in message <hfor19$9sk$1@news.eternal-september.org>...
> > > andrea182 wrote:
> > > > "Matt Fig" <spamanon@yahoo.com> wrote in message <hfok1a$mac$1@fred.mathworks.com>...
> > > >> There is no way that should take 3 minutes, unless you are letting each step of the loop dump to the screen. Even then it shouldn't take that long. Put a semicolon on your array creation!
> > > >>
> > > >>
> > > >> lungo = 5000;
> > > >>
> > > >> tic
> > > >> for t = lungo:-1:1
> > > >> tsc(1,t)=(t-1)*(-60); % Notice the semicolon
> > > >> end
> > > >> toc
> > > >>
> > > >> tic
> > > >> Y = (0:lungo-1)*(-60);
> > > >> toc
> > > >>
> > > >> isequal(tsc,Y)
> > > >>
> > > >>
> > > >>
> > > >> Elapsed time is 0.000158 seconds.
> > > >> Elapsed time is 0.000101 seconds.
> > > >> ans =
> > > >> 1
> > > >
> > > > i put the semicolon but it takes me a lot of time...it always write
> > > > all the passage on the command window...
> > >
> > > There's something you're not showing us then -- the trailing semicolon
> > > _will_ stop display.
> > >
> > > --
> > that's all the code
> > clear all
> > load test.mat;
> > sc=derivato;
> > lungo=length(sc);
> > for t = lungo:-1:1
> > tsc(1,t)=(t-1)*(-60); % Notice the semicolon
> > end
> > tsc=flipud(tsc);
> > %tsc=composite(:,1); %% tempi
> > sigma_l=std(diff(sc(1:end)));
>
> You could replace the FOR loop with just
>
> tsc(1,:) = 0:-60:-299940;
>
> (and by the way, this is a bit more accurate).
>
> But this does not really speed up your code. As Matt Fig said, there should not be any output with each pass of the loop.
>
> Yuri

if i use this way is muche better....

thanks a lot

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