Waiting for the waitbar

I love matlab waitbar. I use it everywhere in my matlab code. I always have the feeling my code is running faster when a nice window show the progress of calculations being done. What a surprise I had  when I realized that Matlab waitbar function was significantly slowing down all my calculations.

for i=1:EndOfLoop

If you run the following line while the Profiler is On, you will be very surprised. On my computer, a single update of the waitbar takes 30 ms. Now you will think that is not so much, but it is actually A LOT. As you can see just browsing through a for loop with 200 iterations takes 6 seconds ONLY because of the waitbar. This is rather slow.

A simple 100×100 matrix multiplications element wise (so about 10^4 multiplications at each iterations) take 100 times less time!

I did try to speed this up by looking at lower levels calls to the waitbar but Mathworks made it so nice and secure that you can’t access these.

So here is a work-around solutions (Until Mathworks actually fixes this issue). This is not of the best elegance but it works reliably : just limit your access to the function to just what is needed to inform you or your users of the current progress.



for i=1:EndOfLoop
if (round(i/dividerWaitbar)==i/dividerWaitbar)

This code just work out the biggest divider to limit waitbar access and keep  the number of calls to waitbar always in the tens whatever the number of iterations. The use of the logarithmic function is just to make sure it works as well for big or small numbers below 10.

NOTA : I submitted a bug report to Matlab about this issue as this is not a normal behavior. It was confirmed as an enhancement request. We will see if it is incorporated in Matlab 2013. In the meantime, I also recommend using multiWaitbar on the file exchange (from Mathworks actually) which is far more efficient, provides a better look and is more flexible. 

This entry was posted in Annoyances, Intermediate, Making interfaces, Optimizing your code. Bookmark the permalink.

5 Responses to Waiting for the waitbar

  1. I was also annoyed by the waitbar slowness. In case you have no GUI (so you directly print on the console) I find better to use text-based progress bars, e.g.

    Very nice blog by the way,

    • Jerome says:

      I really encourage you to try out the multiWaitbar. It’s quite reasonable in speed.

      There is an internal check for only updating the waitbar for steps that increment more than one pixel on the screen (the standard matlab bar actually updates all the time even when the bar doesn’t need to move).

      • I will give it a try. 🙂
        By the way, on my PC (Windows 7, Chrome 31.0.1650.63 m) every time I load a page a popup appears telling me “Can’t find the brush for: matlabkey”. I do not know if the problem was already signaled by someone else.

  2. Ekran says:

    operations that try to update something on the screen are usually very slow, not only the waitbar in matlab, but in general if one wants to print sth on the screen or plot sth in lower level languages such as C# or so. I suppose this is because the update speed of the screen is around 25Hz and calling a function that can work with 25Hz at most a lot of times with a very higher frequency causes problems. So it is usually not the best idea to try to update the screen a lot of times, but one should rather bring it down to around 25Hz (or whatever the screen update speed is) or do it even slower.
    the way I personally do it is: I monitor the percentage and update the waitbar only if the new percentage is not equal to old percentage. sth like this:
    for n=1:100000
    %update waitbar here

    %do your other stuf…

  3. Ka says:

    Hi, I’ve made a simple example to create progress text on every iteration here : https://www.youtube.com/watch?v=uu3djkry9rU
    and using progress bar here :

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.