Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua - Current time in milliseconds

Tags:

time

lua

Is there a common way to get the current time in or with milliseconds?

There is os.time(), but it only provides full seconds.

like image 946
okoman Avatar asked Jan 20 '09 21:01

okoman


People also ask

How do I get the current time in milliseconds Lua?

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.

What is OS time?

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() .


2 Answers

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).

like image 144
waqas Avatar answered Sep 18 '22 21:09

waqas


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)) 
like image 38
Valerio Schiavoni Avatar answered Sep 21 '22 21:09

Valerio Schiavoni