Clear Filters
Clear Filters

Progress bar takes lots of time

5 views (last 30 days)
tybo1mos
tybo1mos on 5 Feb 2024
Commented: tybo1mos on 5 Feb 2024
I have a parsing script which i'm trying to speed up using the Code Profiler.
The Profiler summary indicates that the wait bar is taking up more than 1/2 the total routine time.
Waitbar is initialized like this:
waitbartotal = finfo.bytes;
fprog = waitbar(0,'Please Wait, Parsing...',...
'CreateCancelBtn','setappdata(gcbf,''canceling'',1)'); % Create a progress bar to show file processing progres
setappdata(fprog,'canceling',0);
When I uncomment the following code
waitbar(file_byte_count/waitbartotal,fprog);
my Rotine takes 227seconds to run vs ~40seconds with this commented out.
The call for the waitbar is located at the bottom of a while statment which reads through a file. Its called 434835 times (in this small example).
Looking for some ideas to show the progress bar more efficiently.

Accepted Answer

John D'Errico
John D'Errico on 5 Feb 2024
Edited: John D'Errico on 5 Feb 2024
Yes, a waitbar takes time to update. It looks like you are calling it a lot. And every time, it does an update. And since this is a graphics object, it takes time, serious time.
A simple solution is to filter those calls. Only update the waitbar infrequently. If I know I am going to call it roughly 100000 times, I'll skip the call almost always. Update it once every 100 or 1000 or 10000 times in your progress.
You can do that by writing a wrapper for the waitbar. It can use a persistent counter variable that is updated, but only actually calls the waitbar every x number of calls. This would be easy to write.
  1 Comment
tybo1mos
tybo1mos on 5 Feb 2024
Thanks. I ended up doing something like this. Might not be the ideal way, but definetly speed things up
tmp=round((file_byte_tot/waitbartotal)*100,0); % Round progress to the nearest 1 perecent
if tmp~=waitbartmp % If precentange has chagned, then update progress bar
waitbar(tmp/100,fprog);
waitbartmp=tmp;
end

Sign in to comment.

More Answers (0)

Categories

Find more on Dialog Boxes in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!