How can I reduce minimum wait() time?

10 views (last 30 days)
gujax
gujax on 20 Jan 2023
Answered: Yoga on 10 Mar 2023
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 Comments
gujax
gujax on 20 Jan 2023
Edited: gujax on 20 Jan 2023
What decides this minimum time? Is it the operating system or Matlab? So if my object function gets processed faster than 1 msec, is wait command not going to return Matlab for further commands to be processed until the end of 1 ms? How can I reduce the wait time further? What other methods exists?

Sign in to comment.

Accepted Answer

Yoga
Yoga on 10 Mar 2023
Hi @gujax!
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.

More Answers (0)

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!