How can I reduce minimum wait() time?
10 次查看(过去 30 天)
显示 更早的评论
I am using wait(obj,state,time-out) minimum timeout is 0.01. If I use any value below 0.01, it defaults to 0.01 seconds. Is there any way I can change this functionality to reduce this to 0.001 seconds or less?
3 个评论
采纳的回答
Yoga
2023-3-10
The reason is that Windows task scheduler resolution is the order of 15 ms in some recent versions. So it takes a bit of work arounds to actually get this task done.
I see two options. Let's call them the looping option and the native option. The first is just using a while loop to check if your desired time to wait is already reached. You can do this with MATLAB's stopwatch timer tic and toc. This is the normal time (not the CPU-time). As you are writing a loop, which runs at maximum speed, you might encounter a high CPU-usage but this should be OK if it is only for a couple of milliseconds.
%% looping
% desired time to wait
dt_des = 0.001; % 1 ms
% initialize clock
t_st = tic;
% looping
while toc(t_st) < dt_des
end
% read clock
toc(t_st)
The native option is using pause (make sure that you have enabled it once with pause('on')). I assumed from your question that this does not always work -- however, it does on my system (R2019b, windows 10).
%% use pause()
tic
pause(0.001)
toc
The results of both are -
Elapsed time is 0.001055 seconds.
Elapsed time is 0.001197 seconds.
It's not too accurate but you might get better results if you tune the numbers on your PC.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!