Lua微秒睡眠 [英] Lua microseconds sleep

查看:118
本文介绍了Lua微秒睡眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我需要在lua脚本中等待(例如)10个我们.有什么办法吗?

Ok, I need to wait (for instance) for 10 us in my lua script. Is there any way to do it?

现在,我使用socket使用此简单(非繁忙等待)功能:

For now I use this simple (non busy-waiting) function using socket:

function my_utils.sleep(sec)
    socket.select(nil, nil, sec)
end

但是它的睡眠最小时间"似乎是毫秒.

but it seems its "sleep minimum" is milliseconds.

示例1:

for x=0,2*math.pi,0.008 do
    misc.printus()
    misc.sleep(0.001)
end

输出:

0.022654
0.023654
0.024654
0.025654
0.026654
0.027654
0.028654
0.029654
0.030654
0.031654
0.032654
...

还不错!

示例2:

for x=0,2*math.pi,0.008 do
    misc.printus()
    misc.sleep(0.0001)
end

输出:

0.537089
0.537089
0.537089
0.537089
... --(lots of)
38089
0.538089
0.538089
0.538089
0.538089
0.538089
0.538089
... --(lots of)
0.538089
0.577091 -- (jumps over 0.04 second!)
0.577091
0.577091
0.577091
0.577091
... --(and so on)

有人可以给我一些解释(大约跳0.04秒)吗?

Could someone give me some explanations (and about 0.04 second jump)?

推荐答案

Lua使用操作系统提供的时间服务.可用的分辨率通常不是很高,并且被认为是Lua级别上的时间跳跃.

Lua uses the time service provided by the operating system. The available resolution is usually not very high and it's perceived as time jumps at Lua level.

想象一下具有分钟分辨率的壁钟.它不能用于测量毫秒间隔.

Imagine a wall clock with minute resolution. It cannot be used to measure millisecond intervals.

这篇关于Lua微秒睡眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆