Is there a common way to get the current time in or with milliseconds?
There is os.time()
, but it only provides full seconds.
Another method, to build a timestamp with milliseconds, is to use the milliseconds from the time reported by os. clock() . The os. clock() function returns the CPU time since Lua started in seconds, with milliseconds precision.
os.time() prints the number of seconds passed from the Unix Time Epoch (1st January, 1970, midnight) and not the current time / date. For getting current time/date, use os.date() .
I use LuaSocket to get more precision.
require "socket" print("Milliseconds: " .. socket.gettime()*1000)
This adds a dependency of course, but works fine for personal use (in benchmarking scripts for example).
If you want to benchmark, you can use os.clock as shown by the doc:
local x = os.clock() local s = 0 for i=1,100000 do s = s + i end print(string.format("elapsed time: %.2f\n", os.clock() - x))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With