A few days ago, blog reader Tomás Peyré commented on the fact that the pause function has a memory leak on R2011b and R2012a, and advised using the Java
Thread.sleep() function instead. This got me started on a comparison between these seemingly-equivalent functions. It turns out that the Java variant not only does not leak memory like its Matlab counterpart pause – the Java function is also much more accurate in its pause duration.
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%|
||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%|
||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 increases as the pause duration decreases. The effect of increasing absolute inaccuracy with decreasing total pause duration is catastrophic. On the other hand, Java’s inaccuracy remains stable at <1 mSec, making its relative inaccuracy degradation much more gradual and acceptable (8% at 0.01, 20% at 0.005).
Matlab’s pause function has an important side-effect, that may partly explain the discrepancy: Whenever pause is called, the graphics event queue (EDT) is flushed, thereby updating all Matlab figure windows. In my tests above I did not have any open figures, otherwise I suspect the discrepancy would only have been larger. This side effect is actually quite important when integrating Java GUI components in Matlab figures.
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 pause. Sleep() over it…