Timers: possible bug/documentation error
1 view (last 30 days)
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:
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)
h = timer('TimerFcn', @(src, evt)toc, 'ExecutionMode', 'FixedRate');
for ii = 1:5e2
x = randn(1000000, 1);
y = x(1:500000);
y = [y x(500001:1000000)];
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.
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.
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