Skip to content

Instantly share code, notes, and snippets.

@perusio
Forked from calio/clock_gettime
Created November 4, 2015 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perusio/efd578483f476b75224b to your computer and use it in GitHub Desktop.
Save perusio/efd578483f476b75224b 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