Skip to content

Instantly share code, notes, and snippets.

@calio
Last active January 11, 2018 13:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save calio/49653917ba24e4bec067 to your computer and use it in GitHub Desktop.
Save calio/49653917ba24e4bec067 to your computer and use it in GitHub Desktop.
clock_gettime() via LuaJIT + FFI
local ffi = require("ffi")
ffi.cdef[[
typedef long time_t;
typedef int clockid_t;
typedef struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
} nanotime;
int clock_gettime(clockid_t clk_id, struct timespec *tp);
]]
function clock_gettime()
local pnano = assert(ffi.new("nanotime[?]", 1))
-- CLOCK_REALTIME -> 0
ffi.C.clock_gettime(0, pnano)
return pnano[0]
end
local nano = clock_gettime()
print(nano.tv_sec)
print(nano.tv_nsec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment