Thread Subject: time consuming 'waitbar'

Subject: time consuming 'waitbar'

From: arun

Date: 22 Sep, 2009 13:50:54

Message: 1 of 9

I really don't understand this.

My simulation (with a for-loop over 240000 times) takes about 21
seconds to complete.
I use disp(i); inside to see how much is remaining.

Instead, I tried to use
h = waitbar(0, 'please wait...');

for i=1:iy,
   waitbar(i/iy, h);
   ....
end

And it took 576 seconds!!! What is wrong here?

best, arun.

Subject: time consuming 'waitbar'

From: Sebastiaan

Date: 22 Sep, 2009 14:14:21

Message: 2 of 9

GUI updates and wais. Use
profile on
<run your code>
profreport

to check how much time is spend in the functions.

I prefer to display progress in the command window, like:

Total = 240000;
fprintf('Please wait: %3d%%', 0);
for j=1:Total
    fprintf(sprintf('%s%%3d%%%%', repmat('\b', 1, 4)), round(j/Total*100));
end
fprintf('. Done!\n')

Or:
places = ceil(log10(Total+1));
totalplaces = places*2 + 1;
fprintf(sprintf('Please wait: %%%dd/%d', places, Total), 0);
for j=1:Total
    fprintf(sprintf('%s%%%dd/%d', repmat('\b', 1, totalplaces), places, Total), j);
end
fprintf('. Done!\n')


It basically uses the backspace character to erase the previous characters and overwrites them. The only disadventage is that the logfile (-logfile at startup) gets bloated by \b's, so you need a program or editor that interpretates the \b correctly.

Sebastiaan


arun <aragorn168b@gmail.com> wrote in message <ec5f109b-3d69-4083-81cd-c34e0087b0a0@l35g2000vba.googlegroups.com>...
> I really don't understand this.
>
> My simulation (with a for-loop over 240000 times) takes about 21
> seconds to complete.
> I use disp(i); inside to see how much is remaining.
>
> Instead, I tried to use
> h = waitbar(0, 'please wait...');
>
> for i=1:iy,
> waitbar(i/iy, h);
> ....
> end
>
> And it took 576 seconds!!! What is wrong here?
>
> best, arun.

Subject: time consuming 'waitbar'

From: arun

Date: 22 Sep, 2009 14:22:58

Message: 3 of 9

On Sep 22, 4:14 pm, "Sebastiaan"
<s.breedv...@erasmusmc.REMOVE.BOO.BOO.nl> wrote:
> GUI updates and wais. Use
> profile on
> <run your code>
> profreport
>
> to check how much time is spend in the functions.
>
> I prefer to display progress in the command window, like:
>
> Total = 240000;
> fprintf('Please wait: %3d%%', 0);
> for j=1:Total
>     fprintf(sprintf('%s%%3d%%%%', repmat('\b', 1, 4)), round(j/Total*100));
> end
> fprintf('. Done!\n')
>
> Or:
> places = ceil(log10(Total+1));
> totalplaces = places*2 + 1;
> fprintf(sprintf('Please wait: %%%dd/%d', places, Total), 0);
> for j=1:Total
>     fprintf(sprintf('%s%%%dd/%d', repmat('\b', 1, totalplaces), places, Total), j);    
> end
> fprintf('. Done!\n')
>
> It basically uses the backspace character to erase the previous characters and overwrites them. The only disadventage is that the logfile (-logfile at startup) gets bloated by \b's, so you need a program or editor that interpretates the \b correctly.
>
> Sebastiaan
>
> arun <aragorn1...@gmail.com> wrote in message <ec5f109b-3d69-4083-81cd-c34e0087b...@l35g2000vba.googlegroups.com>...
> > I really don't understand this.
>
> > My simulation (with a for-loop over 240000 times) takes about 21
> > seconds to complete.
> > I use disp(i); inside to see how much is remaining.
>
> > Instead, I tried to use
> > h = waitbar(0, 'please wait...');
>
> > for i=1:iy,
> >    waitbar(i/iy, h);
> >    ....
> > end
>
> > And it took 576 seconds!!! What is wrong here?
>
> > best, arun.

Hi Sebastiaan,

Thank you for your reply. But this itself takes about 1/2 a minute to
execute. My code runs for around 20 seconds without any display and
about 32 seconds with disp(i);
By using this it goes to about a minute and a half to 2 minutes. which
is not acceptable as i have to run this again a 1000 times.

Total = 240000;
fprintf('Please wait: %3d%%', 0);
for j=1:Total
    fprintf(sprintf('%s%%3d%%%%', repmat('\b', 1, 4)), round(j/
Total*100));
end
fprintf('. Done!\n')

However, it looks clean. I will stick with disp(i); for now i guess.I
don't know why it takes so much time to know where it is than the
actual calculations!

best, arun.

Subject: time consuming 'waitbar'

From: Bruno Luong

Date: 22 Sep, 2009 14:35:21

Message: 4 of 9

arun <aragorn168b@gmail.com> wrote in message <df998865-b878-42d4-ba80-cd8fcbfaa5bd@o13g2000vbl.googlegroups.com>...
> On Sep 22, 4:14?pm, "Sebastiaan"
> <s.breedv...@erasmusmc.REMOVE.BOO.BOO.nl> wrote:
> > GUI updates and wais. Use
> > profile on
> > <run your code>
> > profreport
> >
> > to check how much time is spend in the functions.
> >
> > I prefer to display progress in the command window, like:
> >
> > Total = 240000;
> > fprintf('Please wait: %3d%%', 0);
> > for j=1:Total
> > ? ? fprintf(sprintf('%s%%3d%%%%', repmat('\b', 1, 4)), round(j/Total*100));
> > end
> > fprintf('. Done!\n')
> >
> > Or:
> > places = ceil(log10(Total+1));
> > totalplaces = places*2 + 1;
> > fprintf(sprintf('Please wait: %%%dd/%d', places, Total), 0);
> > for j=1:Total
> > ? ? fprintf(sprintf('%s%%%dd/%d', repmat('\b', 1, totalplaces), places, Total), j); ? ?
> > end
> > fprintf('. Done!\n')
> >
> > It basically uses the backspace character to erase the previous characters and overwrites them. The only disadventage is that the logfile (-logfile at startup) gets bloated by \b's, so you need a program or editor that interpretates the \b correctly.
> >
> > Sebastiaan
> >
> > arun <aragorn1...@gmail.com> wrote in message <ec5f109b-3d69-4083-81cd-c34e0087b...@l35g2000vba.googlegroups.com>...
> > > I really don't understand this.
> >
> > > My simulation (with a for-loop over 240000 times) takes about 21
> > > seconds to complete.
> > > I use disp(i); inside to see how much is remaining.
> >
> > > Instead, I tried to use
> > > h = waitbar(0, 'please wait...');
> >
> > > for i=1:iy,
> > > ? ?waitbar(i/iy, h);
> > > ? ?....
> > > end
> >
> > > And it took 576 seconds!!! What is wrong here?
> >
> > > best, arun.
>
> Hi Sebastiaan,
>
> Thank you for your reply. But this itself takes about 1/2 a minute to
> execute. My code runs for around 20 seconds without any display and
> about 32 seconds with disp(i);
> By using this it goes to about a minute and a half to 2 minutes. which
> is not acceptable as i have to run this again a 1000 times.
>
> Total = 240000;
> fprintf('Please wait: %3d%%', 0);
> for j=1:Total
> fprintf(sprintf('%s%%3d%%%%', repmat('\b', 1, 4)), round(j/
> Total*100));
> end
> fprintf('. Done!\n')
>
> However, it looks clean. I will stick with disp(i); for now i guess.I
> don't know why it takes so much time to know where it is than the
> actual calculations!
>
> best, arun.

When program in Matlab, think OVERHEAD, OVERHEAD, OVERHEAD, OVERHEAD, ...
 
The total waitbar length is about 300 pixels. Do you need to call it so often (about 1000 times before it can move 1 pixel)?

Make it coarse, try to update it no more than 100 times.

Bruno

Subject: time consuming 'waitbar'

From: Sebastiaan

Date: 22 Sep, 2009 14:36:19

Message: 5 of 9

I just realize that it does not make any sense to update every iteration: not for the waitbar, not for the console. Every 1/100'th iteration of the loop suffices. Here is the code from my testfile:

Total = 240000;


tic
for j=1:Total
    disp(j)
end
toc

tic
for j=1:Total
end
toc
    
tic
fprintf('Please wait: %3d%%', 0);
for j=1:Total
    fprintf(sprintf('%s%%3d%%%%', repmat('\b', 1, 4)), round(j/Total*100));
end
fprintf('. Done!\n')
toc

tic
places = ceil(log10(Total+1));
totalplaces = places*2 + 1;
fprintf(sprintf('Please wait: %%%dd/%d', places, Total), 0);
for j=1:Total
    fprintf(sprintf('%s%%%dd/%d', repmat('\b', 1, totalplaces), places, Total), j);
end
fprintf('. Done!\n')
toc

tic
h = waitbar(0, 'Please wait...');
for j=1:Total
    waitbar(j/Total, h);
end
toc

tic
fprintf('Please wait: %3d%%', 0);
Step = floor(Total/100);
for j=1:Total
    if ~mod(j, Step) || j==Total % Update only whole percents
        fprintf(sprintf('%s%%3d%%%%', repmat('\b', 1, 4)), round(j/Total*100));
    end
end
fprintf('. Done!\n')
toc

tic
places = ceil(log10(Total+1));
totalplaces = places*2 + 1;
fprintf(sprintf('Please wait: %%%dd/%d', places, Total), 0);
for j=1:Total
    if ~mod(j, 100) || j==Total % Update every 100
        fprintf(sprintf('%s%%%dd/%d', repmat('\b', 1, totalplaces), places, Total), j);
    end
end
fprintf('. Done!\n')
toc

tic
h = waitbar(0, 'Please wait...');
Step = floor(Total/100);
for j=1:Total
    if ~mod(j, Step) || j==Total % Update only whole percents
        waitbar(j/Total, h);
    end
end
toc

Subject: time consuming 'waitbar'

From: Doug Schwarz

Date: 22 Sep, 2009 14:36:50

Message: 6 of 9

In article
<ec5f109b-3d69-4083-81cd-c34e0087b0a0@l35g2000vba.googlegroups.com>,
 arun <aragorn168b@gmail.com> wrote:

> I really don't understand this.
>
> My simulation (with a for-loop over 240000 times) takes about 21
> seconds to complete.
> I use disp(i); inside to see how much is remaining.
>
> Instead, I tried to use
> h = waitbar(0, 'please wait...');
>
> for i=1:iy,
> waitbar(i/iy, h);
> ....
> end
>
> And it took 576 seconds!!! What is wrong here?
>
> best, arun.

You are calling waitbar 240000 times and the run time increases by 555
seconds. That's 2.3 ms per call which is not bad considering waitbar is
updating a graph.

Instead, use something like this:

  for i = 1:240000
      % your code
      if rem(i,1000) == 0
          waitbar(i/240000,h)
      end
  end

and waitbar will only be called every thousand iterations.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: time consuming 'waitbar'

From: Sebastiaan

Date: 22 Sep, 2009 14:40:19

Message: 7 of 9

>
> Thank you for your reply. But this itself takes about 1/2 a minute to
> execute. My code runs for around 20 seconds without any display and
> about 32 seconds with disp(i);
20 seconds without display? The empty loop should be finished almost instantly.

But you are correct: my first run in a fresh Matlab was very fast, a second run very slow.

See my previous post for the solution :)

Subject: time consuming 'waitbar'

From: Rune Allnor

Date: 22 Sep, 2009 14:48:48

Message: 8 of 9

On 22 Sep, 15:50, arun <aragorn1...@gmail.com> wrote:
> I really don't understand this.
>
> My simulation (with a for-loop over 240000 times) takes about 21
> seconds to complete.
> I use disp(i); inside to see how much is remaining.
>
> Instead, I tried to use
> h = waitbar(0, 'please wait...');
>
> for i=1:iy,
>    waitbar(i/iy, h);
>    ....
> end
>
> And it took 576 seconds!!! What is wrong here?

It takes a lot of time to access windows and screen
updates. You don't do that more often than than you
absolutely need to. If you know up front the number
of iterations N, update the waitbar _at_most_ every
N/100'th iteration.

Rune

Subject: time consuming 'waitbar'

From: arun

Date: 22 Sep, 2009 16:22:27

Message: 9 of 9

On Sep 22, 4:48 pm, Rune Allnor <all...@tele.ntnu.no> wrote:
> On 22 Sep, 15:50, arun <aragorn1...@gmail.com> wrote:
>
> > I really don't understand this.
>
> > My simulation (with a for-loop over 240000 times) takes about 21
> > seconds to complete.
> > I use disp(i); inside to see how much is remaining.
>
> > Instead, I tried to use
> > h = waitbar(0, 'please wait...');
>
> > for i=1:iy,
> >    waitbar(i/iy, h);
> >    ....
> > end
>
> > And it took 576 seconds!!! What is wrong here?
>
> It takes a lot of time to access windows and screen
> updates. You don't do that more often than than you
> absolutely need to. If you know up front the number
> of iterations N, update the waitbar _at_most_ every
> N/100'th iteration.
>
> Rune

Thanks to all of you. I understand the problem of calling so many
times and the need to update, now.
best, arun.

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com