Luaj os.time()返回毫秒
Luaj中的os.time()以毫秒为单位返回时间,但根据lua文档,它应该以秒为单位返回时间Luaj os.time()返回毫秒,time,lua,luaj,Time,Lua,Luaj,Luaj中的os.time()以毫秒为单位返回时间,但根据lua文档,它应该以秒为单位返回时间 这是Luaj的一只虫子吗 您能推荐一种可以与Luaj(用于java)和RealLua(c/c++)一起使用的解决方案吗?因为我必须为两个应用程序使用相同的lua源。(不能简单地将其除以1000,因为它们都返回不同的时间刻度) 我的lua文件中的示例: local start = os.time() while(true) do print(os.time() - start) end 在C+
local start = os.time()
while(true) do
print(os.time() - start)
end
在C++中,我收到输出:
1
1
1
...(1 seconds passed)
2
2
2
在java中(使用Luaj),我得到:
仅供参考,我在windows上尝试此操作。Lua关于os.time()的手册:
返回的值是一个数字,其含义取决于您的系统。在POSIX、Windows和其他一些系统中,这个数字统计自某个给定的开始时间(“历元”)起的秒数。在其他系统中,未指定其含义,时间返回的数字只能用作os.date和os.difftime的参数
因此,任何Lua实现都可以自由地更改
os.time()
值的含义。是的,luaj中有一个bug
调用os.time()时,实现只返回System.currentTimeMillis()
。它应该返回类似于(long)(System.currentTimeMillis()/1000。)
还值得指出的是,luaj中的os.date和os.time处理几乎完全缺失。我建议您假设它们尚未实现。看起来您已经确认这是LuaJ中的一个bug;至于解决方法,您可以使用自己的版本替换os.time():
if (runningunderluaj) then
local ostime = os.time
os.time = function(...) return ostime(...)/1000 end
end
其中runningunderUAJ
可以检查某些仅在luaj下设置的全局变量。如果这不可用,您可以通过比较对测量时差的os.clock
和os.time
调用的结果来进行自己的检查:
local s = os.clock()
local t = os.time()
while true do
if os.clock()-s > 0.1 then break end
end
-- (at least) 100ms has passed
local runningunderluaj = os.time() - t > 1
注意:
os.clock()。我没有访问luaj的权限来测试这个…在luaj-3.0-beta2中,它被固定为以秒为单位的返回时间
在luaj-3.0-beta1之前的所有版本中,这都是一个bug time()以秒为单位返回时间。你可能把单位和精度搞混了12.423
是以秒为单位的时间,精度为毫秒。os.time()应以秒为单位返回时间。但在Luaj中,它以毫秒为单位返回(我想,至少当我将它改为1000时,感觉像是一秒钟),你能给我们举个例子吗?我认为它返回的时间是以秒为单位的,但精度是毫秒,而你看到的是精度,并将其与单位混淆。@DavidSchwartz我在上面的问题中添加了代码示例“…在POSIX、Windows和其他一些系统中,…计算秒数”,对不起,我应该提到我的系统是什么。我使用windows。所以它应该返回秒数,对吗?@bysreg-关于POSIX、Windows的词只关于香草Lua。Luaj在Windows上不工作,它在Java上工作。;-)似乎没有比使用os.clock()更好的方法了,所以我选择这个作为答案在搜索代码检查您的声明后,我确认luaj实现使用System.currentTimeMillis()实现os.time()(您可以在OsLib.java第308行中看到)os.clock()
返回程序所用CPU时间的近似值(以秒为单位)。@HJBotter92,正确,但它的精度为毫秒,因此计算增量应该可以。
local s = os.clock()
local t = os.time()
while true do
if os.clock()-s > 0.1 then break end
end
-- (at least) 100ms has passed
local runningunderluaj = os.time() - t > 1