**- Undocumented Matlab - https://undocumentedmatlab.com -**

Pause for the better

Posted By Yair Altman On August 29, 2012 | __17 Comments__

A few days ago, blog reader Tomás Peyré commented ^{[3]} on the fact that the * pause* function has a memory leak on R2011b and R2012a, and advised using the Java

`Thread.sleep()`

Testing the pause accuracy is quite simple: Simply pause a pre-defined duration and measure the actual time span. I run this 100 times to get some meaningful statistics. Note that the only difference between the loops below is the pause function that is being used (see highlighted rows):

% Some initializations timesToRun = 100; duration = 0.25; % [secs] % Measure Matlab's pause() accuracy tPause(timesToRun) = 0; % pre-allocate for idx = 1 : timesToRun tic pause(duration); % Note: pause() accepts [Secs] duration tPause(idx) = abs(toc-duration); end % Measure Java's sleep() accuracy tSleep(timesToRun) = 0; % pre-allocate for idx=1 : timesToRun tic java.lang.Thread.sleep(duration*1000); % Note: sleep() accepts [mSecs] duration tSleep(idx) = abs(toc-duration); end

The results are consistent and conclusive:

Function | median inaccuracy | mean inaccuracy | max inaccuracy | max inaccuracy as % of total (250 mSec) |
---|---|---|---|---|

Matlab’s pause() | 2.0 mSec | 3.6 mSec | 12.8 mSec | 5.1% |

Java’s `Thread.sleep()` | 0.01 mSec | 0.02 mSec | 0.53 mSec | 0.2% |

When the pause duration is reduced to 50 mSecs (0.05 secs), the results are even more striking:

Function | median inaccuracy | mean inaccuracy | max inaccuracy | max inaccuracy as % of total (50 mSec) |
---|---|---|---|---|

Matlab’s pause() | 11.6 mSec | 12.5 mSec | 15.5 mSec | 31% |

Java’s `Thread.sleep()` | 0.78 mSec | 0.77 mSec | 0.84 mSec | 1.7% |

When the pause duration is further reduced, Matlab’s inaccuracies increase (120% at 0.01, 370% at 0.005) to a point where we cannot in practice rely on them. Apparently, Matlab’s * pause* function has an inherent inaccuracy of several mSecs that

Matlab’s * pause* function has an important side-effect, that may partly explain the discrepancy: Whenever

Java’s `Thread.sleep`

function, like Matlab’s * pause()*, is not guaranteed to pause for the exact duration specified. However, as the above results clearly show, in practice the Java variant is much more accurate than Matlab’s. So if you want accurate and leak-free pauses, and do not need the EDT side-effect, I really see no reason to use Matlab’s

Categories: Java, Low risk of breaking in future versions, Stock Matlab function

17 Comments (Open | Close)

Article printed from Undocumented Matlab: **https://undocumentedmatlab.com**

URL to article: **https://undocumentedmatlab.com/blog/pause-for-the-better**

URLs in this post:

[1] Image: **http://undocumentedmatlab.com/feed/**

[2] **email feed**: **http://undocumentedmatlab.com/subscribe_email.html**

[3] commented: **https://undocumentedmatlab.com/blog/waiting-for-asynchronous-events/#comment-105879**

[4] `Thread.sleep()`

: **http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html#sleep(long)**

[5] EDT: **https://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/**

[6] quite important: **http://www.mathworks.com/matlabcentral/answers/7309**

[7] Matlab-Java memory leaks, performance : **https://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance**

[8] File deletion memory leaks, performance : **https://undocumentedmatlab.com/blog/file-deletion-memory-leaks-performance**

[9] Converting Java vectors to Matlab arrays : **https://undocumentedmatlab.com/blog/converting-java-vectors-to-matlab-arrays**

[10] Expanding urlread capabilities : **https://undocumentedmatlab.com/blog/expanding-urlreads-capabilities**

[11] Solving an mput (FTP) hang problem : **https://undocumentedmatlab.com/blog/solving-an-mput-ftp-hang-problem**

[12] Using SQLite in Matlab : **https://undocumentedmatlab.com/blog/using-sqlite-in-matlab**

[13] : **http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/**

[14] : **http://www.mathworks.com/support/bugreports/819115**

Click here to print.

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.

17 Comments To "Pause for the better"

#1 CommentBythe cyclistOn September 4, 2012 @ 7:58 amA couple comments:

First, the results may be platform-dependent. Running R2012a on a Mac (OS X 10.6.8), I get more comparable inaccuracies for MATLAB and Java. Mean inaccuracy for MATLAB is about 0.38 msec, and Java is about 0.26 msec. Also, unlike your results, those inaccuracies are roughly constant when I reduce the input duration.

Second, I notice that one does not need to take the absolute value as you did. The error is systematically positive. This is presumably because both MATLAB and Java choose to pause for

at leastthe prescribed duration.#2 CommentByYair AltmanOn September 4, 2012 @ 8:04 am@Tim – Thanks for your comments. Unfortunately, at least on Windows the error is sometimes negative, hence my use of abs().

#3 CommentByAndrewOn September 6, 2012 @ 7:56 amHi Yair,

I have noticed that there are issues with MATLAB hanging up when using

and other modal dialog boxes. These issues are solved by putting a pause after the dialog box is closed. I’m assuming this has to do with the queue being flushed, as you mentioned. Is there a different way to deal with these issues other than using pause?inputdlgThanks!

~Andrew

#4 CommentByAndrewOn September 6, 2012 @ 7:59 amIt looks like using

has the same effect.drawnowThanks!

~Andrew

#5 CommentByYair AltmanOn September 6, 2012 @ 8:07 am@Andrew – thanks for mentioning this. This is indeed the case, and quite possibly deserves a dedicated article because it is such a frustrating and common issue, that exists in many Matlab releases, and because the solution (adding a

orpause) is so frustratingly simple and yet undocumented. As noteddrawnow^{[6]},is similar but not exactly the same asdrawnow. I often use both of them, since I found out by trial-and-error that different situations respond better to either of them, and I never remember which one to use… So I often have lines such as the following scattered around my code (I know it’s ugly, but it works and that’s what matters in the end):pauseTo your question, I do not know of any simpler or better way to solve these hangups.

#6 CommentByAngOn October 17, 2012 @ 1:23 amHi,

Nice article!

It turns out that I could handle an event during pause, but not when I use Thread.sleep.

For example, it’s possible for an event handler to do something to interrupt the while loop here:

Yet, this is not possible when Thread.Sleep is used.

Do you experience the same thing? I’m using Windows 32-bit MATLAB. In any case, I’d like to try and see where I can put Thread.sleep for my future MATLAB programs.

Regards,

YA

#7 CommentByYair AltmanOn October 17, 2012 @ 11:47 am@Ang – yes, I have also seen this. I am not exactly sure why this happens. It is probably

^{[13]}-related, but I am not sure exactly how or why.#8 CommentBykitsOn November 2, 2012 @ 11:55 amTry adding a “drawnow;” in that loop?

#9 CommentByDougOn January 30, 2013 @ 1:18 pmThanks for the `drawnow;` recommendation! It made this all work perfectly for me.

#10 CommentByMarcOn February 5, 2013 @ 3:48 amThe following patch worked for me to correct this memory (and windows handle!) leak on 2012b:

^{[14]}Since the problem seems to be inside

, using sleep+drawnowprobably doesn’t help.drawnow#11 CommentByYair AltmanOn February 5, 2013 @ 4:09 am@Marc – thanks for sharing. According to the patch description, it is not directly related to

, but perhaps the issues are somehow related.pause#12 CommentByMarcOn February 5, 2013 @ 4:38 amAs far as I can see,

calls (a part of)pause()to flush the queue. Interesting: with Process Explorer I found thatdrawnowleaks semaphore handles as well as memory, so the flush probably forgets to clean up a semaphore object…pause()#13 PingbackBySolving a Matlab hang problem | Undocumented MatlabOn February 6, 2013 @ 11:01 am[…] a reader’s comment yesterday happened on the same page where I explained the problem and solution […]

#14 PingbackByTake a break, have a pause() | matlabideasOn May 18, 2013 @ 7:54 am[…] time ago my colleague showed me an article from Undocumented MATLAB that argues in favour of the Java Thread.sleep() function instead of the MATLAB pause() […]

#15 CommentBySometalkOn August 16, 2018 @ 12:53 pmHave you rerun this benchmark more recently? I wonder if there have been any improvements in this space.

#16 CommentByYair AltmanOn August 16, 2018 @ 2:35 pmIn recent Matlab releases, Matlab’s

is no less (sometimes even more) accurate than Java’spause`Thread.sleep`

, with a consistent mean inaccuracy (overhead) of 0.1-0.2 ms and max inaccuracy of ~1ms, regardless of the pause duration. These values depend of course on the specific OS and CPU, so YMMV.#17 CommentByDimaOn May 9, 2019 @ 11:25 amHey Yair,

As you said, in the new MATLAB versions the pause performs the same or even better for some pause duration. The interesting thing is that for time duration in between >1ms and <10ms Java's sleep performs better (see a table below) but for duration less than equal to 1ms MATLAB the opposite is true.

Do you have any idea why is this?

Also does it make any sense at all to measure pausing time for duration less than 1ms? (Can tic/toc give reliable values for this small time period?)

============================================================

Pausing Time = 10ms :

pause() : Median = 5.6129 Mean = 5.3252 Max = 12.1966

sleep() : Median = 5.6125 Mean = 5.4652 Max = 14.849

============================================================

Pausing Time = 7ms :

pause() : Median = 8.6154 Mean = 7.7185 Max = 14.0192

sleep() : Median = 0.3767 Mean = 0.60192 Max = 1.3815

============================================================

Pausing Time = 5ms :

pause() : Median = 10.6026 Mean = 8.8543 Max = 15.4677

sleep() : Median = 0.38281 Mean = 0.57611 Max = 1.4071

============================================================

Pausing Time = 1ms :

pause() : Median = 0.0076145 Mean = 0.015155 Max = 0.31958

sleep() : Median = 0.34215 Mean = 0.53122 Max = 1.5084

============================================================