How can I reduce minimum wait() time?
10 views (last 30 days)
Show older comments
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?
Accepted Answer
Yoga
on 10 Mar 2023
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 Comments
More Answers (0)
See Also
Categories
Find more on Whos 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!