Progress bar takes lots of time

10 次查看(过去 30 天)
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.

采纳的回答

John D'Errico
John D'Errico 2024-2-5
编辑:John D'Errico 2024-2-5
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 个评论
tybo1mos
tybo1mos 2024-2-5
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dialog Boxes 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by