Timers: possible bug/documentation error

1 view (last 30 days)
Daniel Shub
Daniel Shub on 10 Mar 2013
A few years ago I asked this question about timers. I took Walter's answer as gospel and didn't bother testing it since the potential for timers to operate in a different thread made them unsuitable for my application. Per has a recent question on timers where the problem has come up again.
At the root of the problem is that the documentation says
But in the case of a block of code:
y=x(1:500000);
y=[y x(500001:1000000)];
a timer shot can occur after the first statement and change "x".
But if I run (note you may have to adjust the number of iterations to give a reasonable run time)
function test
h = timer('TimerFcn', @(src, evt)toc, 'ExecutionMode', 'FixedRate');
tic; start(h);
pause(2);
for ii = 1:5e2
x = randn(1000000, 1);
y = x(1:500000);
y = [y x(500001:1000000)];
end
pause(2);
stop(h);
delete(h);
end
I get
Elapsed time is 0.001405 seconds.
Elapsed time is 1.001806 seconds.
Elapsed time is 2.002036 seconds.
Elapsed time is 16.791777 seconds.
Elapsed time is 17.001169 seconds.
Elapsed time is 18.001609 seconds.
The timer appears to fire immediately after starting (as you would expect), and twice more during the pause before the loop starts (again as expected). There is then a 14.8 second gap (about the amount of time the loop takes in the absence of the timer) followed by 3 more timer events (again expected given the 2 second pause).
What am I missing? I am using R2011a, did something change in the 16 months following the release of the tech report. Is there new documentation.

Answers (1)

per isakson
per isakson on 10 Mar 2013
Edited: per isakson on 10 Mar 2013
I reproduced your result with R2012a 64Bit, Win7. However, with the accelerator turned off the timer interupt the for-loop as described in the tech-note.
>> feature('accel','off')
>> synchronization_1_2QJ375
Elapsed time is 0.001245 seconds.
Elapsed time is 0.995859 seconds.
Elapsed time is 1.995819 seconds.
Elapsed time is 2.999274 seconds.
Elapsed time is 3.998288 seconds.
Elapsed time is 5.014897 seconds.
Elapsed time is 6.030949 seconds.
Elapsed time is 7.008298 seconds.
Elapsed time is 8.004866 seconds.
...
The function, feature, is documented over at Undocumented Matlab
  2 Comments
Daniel Shub
Daniel Shub on 11 Mar 2013
Edited: Daniel Shub on 11 Mar 2013
I missed the fact that the documentation says the JIT can combine several lines of code. So basically there is no way to know if a timer can interrupt between two particular lines of code. Why in the world they don't just stick timer callbacks into the event queue is beyond me.
Jan
Jan on 11 Mar 2013
@Daniel: We need two kind of timers, one with a queue e.g. triggered by darwnow as usual, and one, which can be expected to fire independently, which can cause serious crashs. Well, let's forget the crashing variant, because it is easy to implement it in a Mex. How could the JIT accelerate code, when a TIMER can inject values?!

Sign in to comment.

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!