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 个评论
gujax
gujax 2023-1-20
编辑:gujax 2023-1-20
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?

请先登录,再进行评论。

采纳的回答

Yoga
Yoga 2023-3-10
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by