Timers: possible bug/documentation error

조회 수: 1 (최근 30일)
Daniel Shub
Daniel Shub 2013년 3월 10일
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.

답변 (1개)

per isakson
per isakson 2013년 3월 10일
편집: per isakson 2013년 3월 10일
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
Daniel Shub
Daniel Shub 2013년 3월 11일
편집: Daniel Shub 2013년 3월 11일
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 2013년 3월 11일
@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?!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by