{"id":2556,"date":"2010-05-14T10:50:08","date_gmt":"2010-05-14T10:50:08","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2010\/05\/14\/progbar\/"},"modified":"2010-05-14T10:50:08","modified_gmt":"2010-05-14T10:50:08","slug":"progbar","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2010\/05\/14\/progbar\/","title":{"rendered":"progbar"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/5021\">Bob<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/869-progbar\">progbar<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/2765\">Ben Mitch<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <p>Do your MATLAB programs take a long time to run? Maybe you have a large simulation that loops over many iterations. It's nice\r\n      to monitor progress. You can do that with <tt><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/waitbar.html\">waitbar<\/a><\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">h = waitbar(0,<span style=\"color: #A020F0\">'Please wait...'<\/span>);\r\n<span style=\"color: #0000FF\">for<\/span> step = 1:1000\r\n  <span style=\"color: #228B22\">% computations take place here<\/span>\r\n  waitbar(step\/1000)\r\n  drawnow\r\n<span style=\"color: #0000FF\">end<\/span>\r\nclose(h)<\/pre><p>That works well for coarse loops with few iterations and considerable time spent during each. The instrumentation itself does\r\n      add some overhead, however. As the number of iterations increases, so does the cumulative time.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">loopSizes = [1 10 100 1000 10e3];\r\nwaitbarTimes = zeros(size(loopSizes));\r\n<span style=\"color: #0000FF\">for<\/span> n=1:numel(loopSizes)\r\n  myBar = waitbar(0,num2str(loopSizes(n)));\r\n  myClock = tic;\r\n  <span style=\"color: #0000FF\">for<\/span> k=1:loopSizes(n)\r\n    waitbar(k\/loopSizes(n),myBar)\r\n    drawnow\r\n  <span style=\"color: #0000FF\">end<\/span> <span style=\"color: #228B22\">%loop simulation<\/span>\r\n  waitbarTimes(n) = toc(myClock);\r\n  close(myBar)\r\n<span style=\"color: #0000FF\">end<\/span> <span style=\"color: #228B22\">%vary problem size<\/span>\r\nplot(loopSizes,waitbarTimes)\r\nxlabel <span style=\"color: #A020F0\">'Loop Size'<\/span>\r\nylabel <span style=\"color: #A020F0\">'Total Time (sec)'<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/progbar_pick_01.png\"> <p>Since a loop of ten thousand iterations added that many seconds of overhead, how many minutes would a loop of one million\r\n      add?\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">100*waitbarTimes(end)\/60 <span style=\"color: #228B22\">% 100X bigger loop (sec-&gt;min)<\/span><\/pre><pre style=\"font-style:oblique\">ans =\r\n       41.332\r\n<\/pre><p>Wow! If your whole simulation only takes a few minutes total to run then you probably don't want to bog it down that much\r\n      by over instrumenting it.\r\n   <\/p>\r\n   <p>You could add logic to your loop to only make updates every 10 iterations for example.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">myBar = waitbar(0,<span style=\"color: #A020F0\">'Big loop with coarse updates'<\/span>);\r\nmyClock = tic;\r\n<span style=\"color: #0000FF\">for<\/span> step=1:1e4\r\n  <span style=\"color: #228B22\">% computations take place here<\/span>\r\n  <span style=\"color: #0000FF\">if<\/span> mod(step,10)==0 <span style=\"color: #228B22\">%every 10-th iteration<\/span>\r\n    waitbar(step\/1e4,myBar)\r\n    drawnow\r\n  <span style=\"color: #0000FF\">end<\/span>\r\n<span style=\"color: #0000FF\">end<\/span>\r\ntoc(myClock)\r\nclose(myBar)<\/pre><pre style=\"font-style:oblique\">Elapsed time is 2.613291 seconds.\r\n<\/pre><p>Of course now your scientific task involves some artistry to chose the right number of iterations to skip each time. However,\r\n      suppose the amount of time varies widely between each iteration. This approach falls apart in that case.\r\n   <\/p>\r\n   <p>What I like about <tt>progbar<\/tt> is that it features an update \"period\" setting you can control. The default is 0.1 seconds. So it will not update more than\r\n      10 times per second. Otherwise, it works (not exactly but) a lot like <tt>waitbar<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">myBar = progbar;\r\nmyClock = tic;\r\n<span style=\"color: #0000FF\">for<\/span> step=1:1e4\r\n  <span style=\"color: #228B22\">% computations take place here<\/span>\r\n  progbar(myBar,100*step\/1e4)\r\n  drawnow\r\n<span style=\"color: #0000FF\">end<\/span>\r\ntoc(myClock)\r\nclose(myBar)<\/pre><pre style=\"font-style:oblique\">Elapsed time is 3.375511 seconds.\r\n<\/pre><p>If we vary the loop size again we can see where <tt>progbar<\/tt> begins to shine.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">progbarTimes = zeros(size(loopSizes));\r\n<span style=\"color: #0000FF\">for<\/span> n=1:numel(loopSizes)\r\n  myBar = progbar;\r\n  myClock = tic;\r\n  <span style=\"color: #0000FF\">for<\/span> k=1:loopSizes(n)\r\n    progbar(myBar,100*k\/loopSizes(n))\r\n    drawnow\r\n  <span style=\"color: #0000FF\">end<\/span> <span style=\"color: #228B22\">%loop simulation<\/span>\r\n  progbarTimes(n) = toc(myClock);\r\n  close(myBar)\r\n<span style=\"color: #0000FF\">end<\/span> <span style=\"color: #228B22\">%vary problem size<\/span>\r\nplot(loopSizes,[waitbarTimes' progbarTimes'])\r\nxlabel <span style=\"color: #A020F0\">'Loop Size'<\/span>\r\nylabel <span style=\"color: #A020F0\">'Total Time (sec)'<\/span>\r\nlegend <span style=\"color: #A020F0\">waitbar<\/span> <span style=\"color: #A020F0\">progbar<\/span> <span style=\"color: #A020F0\">Location<\/span> <span style=\"color: #A020F0\">Northwest<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/progbar_pick_02.png\"> <p>Clearly <tt>progbar<\/tt> adds less overhead to our (hypothetical) simulation. Thanks, Ben!\r\n   <\/p>\r\n   <p><a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2556#respond\">Comments?<\/a><\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_31fcf47662f744888728deda7c00870f() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='31fcf47662f744888728deda7c00870f ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 31fcf47662f744888728deda7c00870f';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Robert Bemis';\r\n        copyright = 'Copyright 2010 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_31fcf47662f744888728deda7c00870f()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.10<br><\/p>\r\n<\/div>\r\n<!--\r\n31fcf47662f744888728deda7c00870f ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/5021 Bob>'s \r\n% pick this week is \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/869-progbar progbar> \r\n% by \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/2765 Ben Mitch>.\r\n%%\r\n% Do your MATLAB programs take a long time to run? Maybe you have a large\r\n% simulation that loops over many iterations. It's nice to monitor progress.\r\n% You can do that with \r\n% |<https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/waitbar.html waitbar>|.\r\nh = waitbar(0,'Please wait...');\r\nfor step = 1:1000\r\n  % computations take place here\r\n  waitbar(step\/1000)\r\n  drawnow\r\nend\r\nclose(h)\r\n%%\r\n% That works well for coarse loops with few iterations and considerable time\r\n% spent during each. The instrumentation itself does add some overhead,\r\n% however. As the number of iterations increases, so does the cumulative\r\n% time. \r\nloopSizes = [1 10 100 1000 10e3];\r\nwaitbarTimes = zeros(size(loopSizes));\r\nfor n=1:numel(loopSizes)\r\n  myBar = waitbar(0,num2str(loopSizes(n)));\r\n  myClock = tic;\r\n  for k=1:loopSizes(n)\r\n    waitbar(k\/loopSizes(n),myBar)\r\n    drawnow\r\n  end %loop simulation\r\n  waitbarTimes(n) = toc(myClock);\r\n  close(myBar)\r\nend %vary problem size\r\nplot(loopSizes,waitbarTimes)\r\nxlabel 'Loop Size'\r\nylabel 'Total Time (sec)'\r\n\r\n%%\r\n% Since a loop of ten thousand iterations added that many seconds of\r\n% overhead, how many minutes would a loop of one million add?\r\n100*waitbarTimes(end)\/60 % 100X bigger loop (sec->min)\r\n%%\r\n% Wow! If your whole simulation only takes a few minutes total to run then you\r\n% probably don't want to bog it down that much by over instrumenting it.\r\n%%\r\n% You could add logic to your loop to only make updates every 10 iterations\r\n% for example.\r\nmyBar = waitbar(0,'Big loop with coarse updates');\r\nmyClock = tic;\r\nfor step=1:1e4\r\n  % computations take place here\r\n  if mod(step,10)==0 %every 10-th iteration\r\n    waitbar(step\/1e4,myBar)\r\n    drawnow\r\n  end\r\nend\r\ntoc(myClock)\r\nclose(myBar)\r\n%%\r\n% Of course now your scientific task involves some artistry to chose the\r\n% right number of iterations to skip each time. However, suppose the amount of\r\n% time varies widely between each iteration. This approach falls apart in that\r\n% case.\r\n%%\r\n% What I like about |progbar| is that it features an update \"period\" setting\r\n% you can control. The default is 0.1 seconds. So it will not update more\r\n% than 10 times per second. Otherwise, it works (not exactly but) a lot like\r\n% |waitbar|.\r\nmyBar = progbar;\r\nmyClock = tic;\r\nfor step=1:1e4\r\n  % computations take place here\r\n  progbar(myBar,100*step\/1e4)\r\n  drawnow\r\nend\r\ntoc(myClock)\r\nclose(myBar)\r\n%%\r\n% If we vary the loop size again we can see where |progbar| begins to\r\n% shine.\r\nprogbarTimes = zeros(size(loopSizes));\r\nfor n=1:numel(loopSizes)\r\n  myBar = progbar;\r\n  myClock = tic;\r\n  for k=1:loopSizes(n)\r\n    progbar(myBar,100*k\/loopSizes(n))\r\n    drawnow\r\n  end %loop simulation\r\n  progbarTimes(n) = toc(myClock);\r\n  close(myBar)\r\nend %vary problem size\r\nplot(loopSizes,[waitbarTimes' progbarTimes'])\r\nxlabel 'Loop Size'\r\nylabel 'Total Time (sec)'\r\nlegend waitbar progbar Location Northwest\r\n%%\r\n% Clearly |progbar| adds less overhead to our (hypothetical) simulation.\r\n% Thanks, Ben!\r\n%%\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2556#respond Comments?>\r\n\r\n##### SOURCE END ##### 31fcf47662f744888728deda7c00870f\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Bob's pick this week is progbar by Ben Mitch.\r\n      \r\n   \r\n   Do your MATLAB programs take a long time to run? Maybe you have a large simulation that loops over many... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2010\/05\/14\/progbar\/\">read more >><\/a><\/p>","protected":false},"author":46,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2556"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2556"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2556\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}